How to calculate number of buckets used by golang hashmap?

65 views
Skip to first unread message

rbala...@gmail.com

unread,
Oct 22, 2019, 1:26:25 AM10/22/19
to golang-nuts
Hi,

I'm trying find the memory usage of a hashmap?

I've got the most of the pointers for calculating memory usage from this issue https://github.com/golang/go/issues/34561

But, I don't know how many buckets that hashmap using to get the accurate result?


Ian Lance Taylor

unread,
Oct 22, 2019, 2:57:55 AM10/22/19
to rbala...@gmail.com, golang-nuts
As far as I know there is no simple way to calculate this precisely,
as the order in which keys are added to the map can cause extra
buckets to be created.

You can approximate based on the load factor, which in the current
implementation is 6.5, meaning that at the point where the map grows
there will be very approximately one bucket (with space for 8 keys)
for each 6.5 keys stored in the map. Until the map grows there will
be more buckets than that. Each time the map grows, the number of
buckets doubles.

Ian
Reply all
Reply to author
Forward
0 new messages