How does one go about finding a memory leak in a production application? For that matter, what if there is no leak, but the application is exhausting memory for non-apparent reasons? Heap profiling can help.
jemalloc's heap profile output files are a functional superset of those created by gperftools, so the gperftools heap profiler documentation is relevant reading. You will need to use the jeprof Perl script that comes with jemalloc rather than the pprof Perl script that is part of the gperftools distribution, because jemalloc uses an incompatible file format to output per thread heap profile data, whereas pprof only understands global heap profile data.
Let's start off with the simple case, where it is possible to shut the application down and see what memory was still allocated at exit. The offending application we will look at is w:
This will result in something like the following output when the program exits:
To learn more about the leaks, run:
To generate a PDF of the call graph for where the leaks occurred, run:
Here is the result: 