March 5, 2013

Memory leaks detection

The subject of memory leaks detection is frequently invoked and yet, lots of people still wonder how to do it! There are a lot of tools on the market for that purpose. I will talk about the two that I am using:
  1. FastMM
  2. madExcept

FastMM

FastMM is actually the memory manager included with Delphi since a few years. It is an original work of Pierre le Riche. Although included with Delphi, you can download full source code from SourceForge at http://fastmm.sourceforge.net

Using FastMM is very easy. There is no install, just copy the files somewhere on your hard disk. You have to add FastMM4 unit as the first unit under the "uses" section in your project's dpr file. Then you recompile the application.

To take fully advantage of FastMM feature, you should open FastMM4Options.inc file and change some conditional compilation directive. This file is heavily commented to explain all the symbols. When developing your application, you should define the symbols "EnableMemoryLeakReporting" and "FullDebugMode".

Defining FullDebugMode symbol cause code doing extensive checking of all memory blocks to be included in the application. All blocks are padded with both a header and trailer that are used to verify the integrity of the heap. Freed blocks are also cleared to ensure that they cannot be reused after being freed. This option slows down memory operations dramatically and should only be used to debug an application that is overwriting memory or reusing freed pointers. Setting this option automatically enables CheckHeapForCorruption and disables ASMVersion. Very important: If you enable this option your application will require the FastMM_FullDebugMode.dll library.

madExcept

madExcept is a tool created by Mathias Rauen. It is an incredibly useful tool, not only for memory leaks, but to help finding the cause of all kind of exceptions.

madExcept is one of the tools in madCollection. It is worth looking for all.

madExcept is free for non-commercial use and not expensive at all for commercial use. Source code is available. See madshi website: http://www.madshi.net/

To use madExcept, you have to install it because it include a "wizard" which is show in the IDE project menu. This wizard will configure the tool and add the required units to your project. The first page of the wizard has most interesting options, notably the one to select for reporting the leaks (memory or other resources).

At the end of program execution, you get a report with all links and required information to find the context in which the leak occurred. See this screen dump:




Follow me on Twitter
Follow me on LinkedIn
Follow me on Google+
Visit my website: http://www.overbyte.be
This article is available from http://francois-piette.blogspot.be/2013/03/memory-leaks-detection.html

1 comment:

David Heffernan said...

madExcept is wonderful. For what it is worth, the leak detection only works for 32 bit processes. The bug reporting tool supports 64 bit too. The leak detection does way more than FastMM. It checks for leaks in system objects as well as Delphi heap memory.