rust-lang / hashbrown

Rust port of Google's SwissTable hash map
https://rust-lang.github.io/hashbrown
Apache License 2.0
2.38k stars 275 forks source link

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

Open iddm opened 6 months ago

iddm commented 6 months ago

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

cuviper commented 5 months ago

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 commented 2 days ago

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.