Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much memory do golang maps reserve?

Given a map allocation where the initial space is not specified, for example:

foo := make(map[string]int)

The documentation suggests that the memory allocation here is implementation dependent. So (how) can I tell how much memory my implementation is allocating to this map?

like image 693
lash Avatar asked Sep 18 '17 11:09

lash


1 Answers

If you look at the source of Go's map type, you will see, that a map consists of a header (type hmap) and an array of buckets (type bmap). When you create a new map and don't specify the initial space (hint), only one bucket is created.

A header consists of several fields:

1 * int,
2 * uint8,
1 * uint16,
1 * uint32,
2 * unsafe.Pointer,
1 * uintptr.

Size of the types int, uintptr, and unsafe.Pointer equals the size of a word (8 bytes on 64 bit machines).

A bucket consists of an array of 8 * uint8.

This gives a total of 40 + 8 = 48 bytes (64 bit architecture)

like image 196
dev.bmax Avatar answered Sep 30 '22 18:09

dev.bmax