How to calculate the size of the hashbrown::HashMap at runtime?
I need to calculate how much memory the hashmap object with all the contents occupies. How could I do that?
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.
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.