0

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?

Arne
  • 2,624
  • 3
  • 24
  • 45

2 Answers2

1

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.

Arne
  • 2,624
  • 3
  • 24
  • 45
1

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

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70