Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Estimating hashmap occupied size #238

Closed
spektom opened this issue Feb 14, 2021 · 3 comments · Fixed by #553
Closed

Estimating hashmap occupied size #238

spektom opened this issue Feb 14, 2021 · 3 comments · Fixed by #553

Comments

@spektom
Copy link

spektom commented Feb 14, 2021

Is there a way to estimate how much memory is occupied by a hashmap instance?

@Amanieu
Copy link
Member

Amanieu commented Feb 15, 2021

Not really. capacity only reports an estimate of how many more elements you can insert before having to resize, but it doesn't tell you how much memory is actually used by the table.

@tgross35
Copy link

tgross35 commented Jun 30, 2022

Is there a good formula to estimate this? I would assume something like:

// HashMap
size_of::<HashMap>() + (size_of::<KeyType>() + size_of::<ValueType>() + size_of::<u64>()) * my_map.capacity()
// HashSet
size_of::<HashSet>() + (size_of::<ValueType>() + size_of::<u64>()) * my_set.capacity()

But I am unsure if there is any extra per-element overhead, if the hash size is stored as u64, or if any of those items are dependent on .len() rather than .capacity().

Also curious how this formula compares with std::collections::HashMap/std::collections::HashSet

@Imberflur
Copy link

There is a RawTable::allocation_info method that seems like it would work for this: https://docs.rs/hashbrown/0.14.5/hashbrown/raw/struct.RawTable.html#method.allocation_info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants