Updated over 3 years ago by Knödlseder Jürgen

How to watch memory usage?

Overall system memory usage

free
watch -n 5 free -m
vmstat

Using valgrind

Either of

valgrind --tool=massif gsrvy
valgrind --tool=massif --massif-out-file=massif.out.%p gsrvy
valgrind --tool=massif --pages-as-heap=yes gsrvy
This produces an output file, for example massif.out.51550 (the second variant is useful for forked processes, the third variant assures that all memory allocated is traced). To visualise the output, type
ms_print massif.out.51550
In the graphs produced, ki is “kilo instructions executed” as a measure of time.
For more information, see https://www.valgrind.org/docs/manual/ms-manual.html.

Note that the gsrvy execution does not need to finish, ctrl-C stops execution and produces the relevant output files.

Generating a call tree

Using valgrind a call tree can be generated using

valgrind --tool=massif --xtree-memory=full gsrvy
which can then be inspected using kcachegrind. The call tree filename is xtmemory.kcg.[pid] which can be opened with kcachegrind.

Investigate memory while running

To investigate the memory usage or call tree while an executable is running, valgrind needs to be used through a gdb server. In one console, start gsrvy using

valgrind --tool=massif --xtree-memory=full --vgdb=yes --vgdb-error=0 gsrvy
This will inform about the command sequence to be used in gdb in a second terminal. Start now gdb in a second terminal:
gdb gsrvy
(gdb) target remote | /usr/libexec/valgrind/../../bin/vgdb --pid=59212
Now the valgrind job can be controlled from gdb.

Starting the job:

(gdb) continue

Creating an intermediate memory call tree:

(gdb) ctrl-C
(gdb) monitor xtmemory
(gdb) continue
See https://www.valgrind.org/docs/manual/ms-manual.html#ms-manual.monitor-commands for all monitor commands.

Note: Instead of starting gdb one can also send commands to valgrind using the vgdb program:

vgdb --pid=59212 xtmemory [<filename> default xtmemory.kcg.%p.%n] requests Massif tool to produce an xtree heap memory report.
vgdb --pid=59212 snapshot [<filename>] requests to take a snapshot and save it in the given <filename> (default massif.vgdb.out).
vgdb --pid=59212 detailed_snapshot [<filename>] requests to take a detailed snapshot and save it in the given <filename> (default massif.vgdb.out).
vgdb --pid=59212 all_snapshots [<filename>] requests to take all captured snapshots so far and save them in the given <filename> (default massif.vgdb.out).
vgdb --pid=59212 v.info memory