Wisconsin CMS: How to find memory leaks#


Memory Leaks#

A memory leak occurs when a program allocates memory but never frees it. The result is that memory usage by the program grows and grows. It can grow so much that it fills the entire machine where the program is running, though the operating system may sometimes kill the program first.#

The best way to prevent memory leaks is to use only local variables that are allocated on the stack, which means they are defined in the standard way and there is no separate allocation (using “new”) on the heap.#

If you must use heap memory, use “std::unique_ptr” and “std::make_unique” to ensure the memory is automatically de-allocated when the pointer goes out of scope.#

In old programs that use “new”, make sure each piece of memory allocated with “new” is later deleted with “delete”.#


Finding memory leaks#

The following tools can help you find memory leaks:#

igprof#

valgrind#

First, build your code with debugging information:#

scram b clean#

scram b USER_CXXFLAGS=“-g”#

More info on [igprof].#

For valgrind, these options might work:#

valgrind –tool=memcheck –leak-check=yes –show-reachable=yes –num-callers=20 –suppressions=$ROOTSYS/etc/valgrind-root.supp –track-origins=yes cmsRunGlibC your-program#

More info on [valgrind].#


Contacts For Help#

Email: help@hep.wisc.edu#