I need to gather statistics about memory usage in my program.
My code is mostly written using STL.
Is there any way of learning how much memory is being consumed by an STL object?
For example,
string s1 = "hello";
string s2 = "hellohellohellohellohellohellohellohellohellohellohellohellohello";
How much memory is consumed by s1 and s2?
Obviously, sizeof(string)+s1.length() is not quite accurate.
If you’re willing to be slightly intrusive, you can make a custom allocator to track all heap usage by container type, or by type allocated. This is quite intrusive, but quite accurate as well, for determining memory usage. This does not track how much memory the heap itself takes though, as that is highly OS-dependent.
And usage is:
Windows results:
http://ideone.com/lr4I8 (GCC) results: