Automatic Leaks Checking Support

This document describes how to check the heap usage of a C++ program. This facility can be useful for automatically detecting memory leaks.

Linking in the Heap Checker

You can heap-check any program that has the tcmalloc library linked in. No recompilation is necessary to use the heap checker.

In order to catch all heap leaks, tcmalloc must be linked last into your executable. The heap checker may mischaracterize some memory accesses in libraries listed after it on the link line. For instance, it may report these libraries as leaking memory when they're not. (See the source code for more details.)

It's safe to link in tcmalloc even if you don't expect to heap-check your program. Your programs will not run any slower as long as you don't use any of the heap-checker features.

You can run the heap checker on applications you didn't compile yourself, by using LD_PRELOAD:

   $ LD_PRELOAD="/usr/lib/" HEAPCHECK=normal 

We don't necessarily recommend this mode of usage.

Turning On Heap Checking

There are two alternatives to actually turn on heap checking for a given run of an executable.