Memory Profiling

Valgrind is a great tool, but it is also very slow, and difficult to setup for use with MPI. An alternative is Google’s gperftools, which is coupled to their own malloc implementation, tcmalloc.

Installation

gperftools is available from github. You may have to install librewind first. Then simply configure (you may want to use the --prefix option), make and make install as normal. To use the output of the heap checker, you’ll also need pprof, available from here. To install it, first install the go compiler (golang-go for Ubuntu), and set GOPATH to something meaningful, like $HOME/pprof. Then, simply type go get -u github.com/google/pprof, and pprof will be installed in $GOPATH/bin. If you can, you may want to put a symlink into /usr/local/bin. Else, an alias in you shell rc-file will also do the trick. Once pprof is installed, you can start using gperftools. Also, make sure graphviz is installed to get pretty graphs later.

Usage

To use the heap checker, you’ll need to use tcmalloc instead of the usual malloc functions. This is easily done with LD_PRELOAD. How to set environment variables depends on your shell, in bash and zsh it can be done by export LD_PRELOAD=<path-to-gperftools>/lib/libtcmalloc.so. Remember to unset LD_PRELOAD once you’re done. To enable the heapchecker, you’ll also have to set HEAPCHECK to normal. You will know when it’s active. It can be disabled again by unset HEAPCHECK.

When running a program with the checker enabled, it will generate a heap profile in /tmp. These can be examined with pprof. You may need to start pprof from a specific directory to make it find the source files. Once you’ve opened the profile, you can get basic info with text, which should help you identify false positives. To get more info on specific entries, you can use list <function>, which will show annotated source code of that function. You can also type evince to generate and show a neat graph of all your leaking allocations.