How Much Memory Do Go Maps Reserve?
In Go, maps are allocated dynamically, with the initial capacity left unspecified. This means that the memory allocation is implementation-specific. To determine the initial memory allocation, consult the map's internal structure.
Map Structure
A map consists of a header (type hmap) and an array of buckets (type bmap). When created without an initial capacity, a single bucket is allocated.
Header Structure and Size
The header comprises:
The size of int, uintptr, and unsafe.Pointer is equal to the word size (8 bytes on 64-bit machines). Therefore, the header size is 40 bytes.
Bucket Structure and Size
Each bucket contains an array of 8 * uint8. Adding this to the header size, we get the total initial memory allocation:
Header Size (40 bytes) + Bucket Size (8 bytes) = **48 bytes (64-bit architecture)**
The above is the detailed content of How Much Memory Does a Go Map Initially Reserve?. For more information, please follow other related articles on the PHP Chinese website!