On Mon, 7 Nov 2016 at 09:32 Ram <sourceopen@gmail.com> wrote:
Thanks Juan! What I am interested in here is mainly to get the size of the housekeeping. The key(string) and the value(pointers to class objects) are of reasonable size and that is fine. I want to see how much overhead the housekeeping causes when my unordered_map is huge. I am trying to decide between a std::map and boost::unordered_map. 


A strategy that might give you what you want is to overload global new and delete and then write some code to log the allocations. From there you can calculate the total amount of memory consumed and subtract out the expected amount of memory from the logged data. Something like the code below is close, but keep in mind that this is an example and there are many overloads for new and delete (nothrow versions, etc) so you will need to make sure that these are the versions being called. This test also doesn't account for allocator overhead and will probably only work on Windows because it uses the platform specific _msize function. On Linux, malloc_usable_size might do the trick instead of _msize.

#include <map>
#include <iostream>
#include <malloc.h>

std::size_t g_TotalMem = 0;
void* operator new(std::size_t size)
{
    g_TotalMem += size;
    return malloc(size);
}

void* operator new[](std::size_t size)
{
    g_TotalMem += size;
    return malloc(size);
}

void operator delete(void* p)
{
    g_TotalMem -= _msize(p);
    free(p);
}

void operator delete[](void* p)
{
    g_TotalMem -= _msize(p);
    free(p);
}

int main()
{
    std::map<int, int> map;
    std::size_t start_mem = g_TotalMem;
    for(int i = 0; i < 10; ++i)
    {
        map.insert({i, i});
    }

    std::cout << g_TotalMem - start_mem << std::endl;
    return 0;
}

Good luck!

-- chris