FASTMM and how to use in a Delphi Project.


One of the most challenging parts of inheriting a legacy project is to fix the memory leaks that most often are hiding in the code. A while ago, while dealing with an application that managed to eat all the available memory within a few hours I found FastMM and it was sure a great find.

What is FastMM?
FastMM is a memory manager replacement designed to be used with Delphi. It is an Open Source project developed by Pierre Le Riche in South Africa. Starting with Delphi 2006 FastMM replaced the Borland memory manager. Unfortunately, Delphi only ships with a subset of FastMM. Most of the useful debugging reporting that can be done with FastMM has been stripped from the shipping version of Delphi, RAD Studio and BDS. But fear not! It is very simple to replace the stripped-down version of FastMM with the full version. All you have to do is follow the directions outlined below. I have also included a step to install the FastMM4 Options Interface program. That program is a very friendly way of configuring the options contained in the file FastMM4Options.inc.

How to install FastMM?
1. Download the latest FastMM source code from http://sourceforge.net/projects/fastmm/

2. Copy the content of the downloaded zip file to a folder on your computer.

3. In Delphi add a path in Tools>Options>Library – Win32 Library Path to the FastMM folder that contains the unit FastMM4.pas.

4. Copy the file FastMM_FullDebugMode.dll from the folder FastMM\FullDebugMode DLL\Precompiled to the Delphi install folder. For example in Delphi 2007 - C:\Program Files\CodeGear\RAD Studio\5.0\bin or in the Delphi XE install folder - C:\Program Files\Embarcadero\RAD Studio\8.0\bin

5. Optionally one can download and install the FastMM4 Options Interface program from JED software’s web site at 
http://jedqc.blogspot.com/2007/07/new-fastmm4-options-interface.html.


Configuring FastMM
Once you have properly installed FastMM4 you will be able to detect any memory leaks and attempts to use freed memory. 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". And If you enable this option your application will require the FastMM_FullDebugMode.dll library. In the FastMM folder you will find the file FastMM4Options.inc. This is the file that controls how FastMM behaves. Each option is very well documented and it is how you set the default behavior of FastMM. One can manually edit this file or, optionally, use the FastMM4 Options Interface.

Let’s take a quick look to this file. There are five different sections in this file.

1. Miscellaneous Options
This section contain general settings to control memory alignment, use of fast move library, multi threaded behavior and debug only when running the IDE



2. Debugging Options
This section contains defines that control the debugging behavior of FastMM such as logging errors to a log file, dumping of memory along with an error, stack traces and more.



3. Memory Leak Reporting
This section controls the reporting of memory leaks, how to deal with expected memory leaks and the presence of the IDE or debug info to report errors.



4. Instruction Set Options
This section deals with using MMX instructions and this option currently only affects the variable size move routines.



5. Memory Manager Sharing Options
This section allows sharing of the memory manager between a main applications and DLLs.



Preparing and compiling projects in Delphi
There are a few simple steps to prepare your existing projects to use the full version of FastMM in a useful way.

1. You can control how FastMM behaves in two ways:
a. By making changes to Option Grouping in the FastMM4Options.inc so you can define different FastMM behaviors for your release version and debug version.
b. Or by using a conditional IFDEF statement to use the complete library on the debug version and ship the Delphi supplied library in the release version. To accomplish this, in Delphi open a project and add the unit FastMM4 as the first unit in the uses clause of the .dpr file.
uses
{$IFDEF DEBUG}
FastMM4,
{$ENDIF}
DB,

2. If your project includes EXEs and DLLs you also need to define ShareMM, ShareMMIfLibrary and AttemptToUseSharedMM using the FastMM4 Options Interface program and add FastMM4.pas to the top of the uses section of the .dpr for both the main application and the DLL and follow the directions outlined in item 3. This will allow FastMM to report memory leaks across EXEs and DLLs.

3. Optionally, set FatsMM4 debug options using the FastMM4 Options Interface program. Make sure to build your program every time you make changes to any FastMM4 options.

4. In order for certain debug features of FastMM4 to work you must make sure that certain debug switches are turned on. The following is a list of recommended switches:
a. In the Compiler options set the following options
·         Debug Information
·         Reference Info
·         Use Debug DCus




b. In the Linker options make sure that one of the following options is set
·         TD32 Debug info
·         Map file




5. After you run the application a log of the memory manager can be found in the same folder where the application ran. The log file is named Leaks_MemoryManager_EventLog.txt.



Comments

  1. I really enjoy your blogs Jitenrdra, especially this one and the one concerting Delphi Memory Management. They are very well written and hugely informative. Thank you.

    ReplyDelete
  2. Actually you don't need "TD32 Debug info" at all.
    And "Use Debug DCus" is also ONLY optional.
    ________
    And I think "Debug Information" is also not necessary, but this I have to test. I am not sure of it.

    ReplyDelete

Post a Comment

Popular posts from this blog

MS Excel Automation in Delphi

ShellExecute in Delphi

Drawing Shapes in Delphi