Home >Backend Development >Golang >How Much Memory Does an Unspecified Go Map Allocate?
When creating a Go map without specifying the initial memory allocation, the actual memory usage remains a mystery. This article sheds light on how to determine the memory allocated by the implementation.
Delving into the source code of the Go map type, we discover that a map comprises two components: a header (hmap) and an array of buckets (bmap). Remarkably, a single bucket is allocated if the initial allocation is unspecified during map creation.
The header consists of seven fields:
On 64-bit machines, the size of int, uintptr, and unsafe.Pointer is 8 bytes each, giving a total of 40 bytes for the header.
A bucket is an array with eight elements of type uint8, resulting in an additional 8 bytes.
Thus, the memory allocated for a map with one bucket consists of:
This calculation is applicable to 64-bit architectures, and the actual size may vary slightly on different platforms.
The above is the detailed content of How Much Memory Does an Unspecified Go Map Allocate?. For more information, please follow other related articles on the PHP Chinese website!