I have a huge C++ project which makes use of the standard containers: vector, map, multimap, set, etc. Platform is Linux x86_64, compiler is g++ 4.9. I encounter some memory problems with large data sets. Now I am wondering: is there a way to see which instances of which containers take up the most memory? Is there a tool, like valgrind, that can do this? A patched standard library that can gather statistics?
-
1You may find Heaptrack useful : http://milianw.de/blog/heaptrack-a-heap-memory-profiler-for-linux – Jesper Juhl Jun 13 '16 at 06:34
-
1http://stackoverflow.com/questions/7448514/how-can-i-know-how-much-memory-an-stl-object-takes – Arunmu Jun 13 '16 at 06:35
-
@JesperJuhl Heaptrack looks great, as does Massif. Didn't know about that part of valgrind yet. If you would formulate a bit more verbose answer, I would be happy to accept it. – Arne Jun 13 '16 at 06:55
-
@Arne done. Added an answer. – Jesper Juhl Jun 15 '16 at 17:38
2 Answers
I found valgrind massif already to be very useful. I am now using it like this:
valgrind --smc-check=all-non-file --tool=massif --max-snapshots=1000 --vgdb=yes --vgdb-error=0 --massif-out-file=/some/dir/massif-%p.out myprogram
This way I even get a remote gdb console at startup and any time to dump more heap snapshots. After startup, you need to attach to gdb and continue execution:
gdb myprogram
(gdb) target remote | vgdb --pid=pid_of_myprogram
(gdb) continue
At any point you can interrupt and dump a snapshot, additionally to the logfile massif-[pid].out:
^C
(gdb) monitor detailed_snapshot mysnapshot-1.out
(gdb) continue
I need the smc check, since my project contains a JVM. If you don't have self modifying code, you can leave it out. The 1000 snapshots for the output is maybe a bit large. The default is 100.

- 2,624
- 3
- 24
- 45
Heaptrack (http://milianw.de/blog/heaptrack-a-heap-memory-profiler-for-linux) is useful for this purpose. As is the Massif tool in Valgrind. I've used both with great success. You may also want to check out the debug mode of libstdc++ - https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode.html

- 30,449
- 3
- 47
- 70