How to Use Valgrind for Memory Profile

How to profile memory usage?

Use massif, which is part of the Valgrind tools. massif-visualizer can help you graph the data or you can just use the ms_print command.

How can I use valgrind for memory profile

You can use valgrind's Massif tool to get a heap profile. This code is still labelled "experimental", and it does not ship with all versions of valgrind. You may have to download and build from source.

Also note that the heap profile is organized by allocation site, which is a finer granularity than classes. If you need information organized by class, you will have to read the developer documentation and get the machine-readable format, then figure out which allocation sites go with which classes - perhaps with support from your compiler.

Even without support for classes, however, the Massif profile may be useful.

How to measure Valgrind's memory usage?

valgrind tools such as memcheck or helgrind use a lot of memory for
tracking various aspects of your program.
So, it is normal that top shows a lot more memory than what your program
allocates itself.

If you want to have an idea about the memory used by valgrind, you can do:

valgrind --stats=yes ...

The lines following

------ Valgrind's internal memory use stats follow ------

will give some info about valgrind memory usage.

Use valgrind --profile-heap=yes ... to have detailed memory use.

Note that if you do not use the standard malloc library, you might need to use the option --soname-synonyms=... to have tools such as memcheck or helgrind working properly.
to

Valgrind and Memory Leaks

I suggest visiting the Valgrind FAQ:

With Memcheck's memory leak detector, what's the difference between
"definitely lost", "indirectly lost", "possibly lost", "still
reachable", and "suppressed"?

The details are in the Memcheck section
of the user manual.

In short:

  • "definitely lost" means your program is leaking memory -- fix those
    leaks!

  • "indirectly lost" means your program is leaking memory in a
    pointer-based structure. (E.g. if the root node of a binary tree is
    "definitely lost", all the children will be "indirectly lost".) If you
    fix the "definitely lost" leaks, the "indirectly lost" leaks should go
    away.

  • "possibly lost" means your program is leaking memory, unless you're
    doing unusual things with pointers that could cause them to point into
    the middle of an allocated block; see the user manual for some
    possible causes. Use --show-possibly-lost=no if you don't want to see
    these reports.

  • "still reachable" means your program is probably ok -- it didn't free
    some memory it could have. This is quite common and often reasonable.
    Don't use --show-reachable=yes if you don't want to see these reports.

  • "suppressed" means that a leak error has been suppressed. There are
    some suppressions in the default suppression files. You can ignore
    suppressed errors.

How to use valgrind with python?

I found the answer here.

Python also needs to be compiled in debug mode, i.e.

./configure --prefix=/home/dejan/workspace/python --without-pymalloc --with-pydebug --with-valgrind

In addition, numpy has a suppresion file that gets rid of the extra valgrind warnings.

Is it possible to set a baseline memory usage in valgrind for leak detection?

valgrind memcheck allows to do a "differential" leak search. The differential leak search reports the delta between the previous leak search and the current situation.

You can do such a differential leak search using monitor commands with vgdb, either from the shell or from gdb. See https://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.monitor-commands.

You can also use the client request VALGRIND_DO_CHANGED_LEAK_CHECK from your program, see https://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.clientreqs.

Having problems profiling memory in Python program using Valgrind

Instead of having the script launch the interpreter, directly calling it as a parameter to Valgrind solves the problem.

valgrind --tool=massif python my_script.py


Related Topics



Leave a reply



Submit