Finding the size of a large boost::unordered_map

264 Views Asked by At

I would like to find the size of a boost::unordered_map I have that contains a pointer to a class mapped by a std::string. I am doing a sizeof(unordered_map var). Is that right? Would it give me the space it occupies? Including the house keeping it takes up? Wanted to measure it to compare it to a std::map that will hold the same data, which also I would measure by sizeof(std::map var). I would like to know both to decide how much storage each occupies, and which is a better alternative to go with, comparing the speed and space.

Please let me know if my way of calculating the sizes are right and will give me the actual/correct sizes and will help me make the right decision.

Edit 1:

If my way of trying to get the size is wrong, please let me know ways of getting the correct size(inclusive of house keeping)

TIA

-R

2

There are 2 best solutions below

2
On

You're simply not going to be able to reliably calculate the amount of space used up by your map. There's types and space you have no access to.

What you should do is ask a totally different question having to do with the problem you're trying to solve where you think this is necessary.

0
On

The sizeof() operator returns only the size of an object, but not the space it occupies on the heap (dynamically allocated memory). Since maps and strings may very well allocate memory on the heap, this will not help you.

There is no simple way to measure the total memory footprint of certain parts of your program. However, it is not impossible. One option is to use a custom allocator, which records its memory allocation and which you use for all objects related to the entities you want to measure (for the map and its objects including the strings).