hashbrown icon indicating copy to clipboard operation
hashbrown copied to clipboard

How to calculate the size of the hashbrown::HashMap at runtime?

Open iddm opened this issue 1 year ago • 2 comments

I need to calculate how much memory the hashmap object with all the contents occupies. How could I do that?

iddm avatar Feb 19 '24 12:02 iddm

I doubt it will ever be exposed, but for a given version you can inspect the source. The immediate size is just std::mem::size_of::<HashMap<K, V>>(), and the heap size comes from the computed layout here:

https://github.com/rust-lang/hashbrown/blob/3741813402f4e2a0e606c4639e102120506b6e33/src/raw/mod.rs#L233-L276

... where T = (K, V) for a HashMap<K, V>, and buckets can be determined from the reported capacity like:

https://github.com/rust-lang/hashbrown/blob/3741813402f4e2a0e606c4639e102120506b6e33/src/raw/mod.rs#L188-L217

All of that is subject to change, and of course if your K or V types have additional indirect memory then you'll need to account for that too.

cuviper avatar Mar 24 '24 05:03 cuviper

I missed RawTable::allocation_info when I answered before, and you can get access to that through HashMap::raw_table. However, the "raw" feature has been removed in #546 for the upcoming 0.15.

cuviper avatar Sep 05 '24 21:09 cuviper