PDA

View Full Version : How do I find memory leaks?



Josh-TotalView-Tech
07-17-2006, 11:10 AM
The TotalView Memory Debugger can locate your program's memory leaks and display information about them. [/url]Before execution begins, enable the Memory Debugger. (See the Debugging Memory Problems Using TotalView (http://www.etnus.com/Documentation/index.html) Guide. [/url]Run the program and then halt it where you want to look at memory problems. Allow your program to run for a while before stopping execution to give it enough time to create leaks. [/url]From the Memory Debugger Window (invoked using the Tools > Memory Debugging command), select the Leak Detection tab.
http://www.etnus.com/Support/Tips/images/71Memory_Leak_Source.png [/url][br]Select one or more processes in the [b]Process Set area. [/url]Select a view within the Generate View area and click the Generate View button. For example, you might select Source View. [/url]Examine the list. After you select a leak in the top part of the window, the bottom of the window shows a backtrace of the place where the memory was allocated. After you select a stack frame in the backtrace, TotalView displays the statement where the block was created. A backtrace is a list of stack frames. The Memory Debugger displays a list that contains the stack frames that existed when you asked the heap manager to allocate memory.

[/url]The backtrace that the Memory Debugger displays is the backtrace that existed when your program made the heap allocation request. It is not the current backtrace.

The line number displayed in the Memory Debugger Source Pane is the same line number that TotalView displays in the Process Window Source Pane. If you go to that location, you can begin devising a strategy for fixing the problem. Sometimes you get lucky and the fix is obvious. In most cases, it isn't clear what was (or should be) the last statement to access a memory block. Even if you figure it out, it's extremely difficult to determine if the place you located is really the last place your program needs this data. At this point, it just takes patience to follow your program's logic.