Memory Leaks and Exeption in Delphi Applications
Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. Languages like C#.Net and Java has their own garbage collectors.
Garbage collection reduces the programmer’s burden of explicitly freeing the objects but at the expense of performance. Delphi do not have any inbuilt Garbage collection mechanism as it prioritize performance.
In Delphi you don’t have to free the components dropped on a form since the parent is responsible to free the memory allocated to its child controls. The controls like button, label dropped on the Form are the child controls of the Form and are freed when the form is closed.
If the controls are created dynamically using ‘Create’ keyword, have to be explicitly freed using ‘Free’ keyword. If you don’t free the memory heap allocated to an object, there are great chances of memory leak to occur. If the application is running continuously for a longer time then there are chances of system getting hanged and eventually going to a not responding state.
What is the difference between Free and Nil?
Free - Deallocates the memory previously allocated for the object
Nil - Dereferences the object
When an object is created like Query1 := TQuery.Create(nil); It creates the instance and allocate some memory space to hold the data of Query1
When Free is called, it frees the memory of space already allocated for Query1
When nil is called, it sets the pointer to nil
Thus, always try to use FreeAndNil instead of Free And Nil separately
When application is small there are no issues with the memory. As application become large and complex, memory issues starts popping up. Hence, Memory management is a priority to any application that we build. Memory leaks are hard to find in code so there comes a need for an external tool. Tools available for managing memory related problems are discussed below:
 • FastMM
FastMM is a Delphi tool to address memory leaks in Delphi applications. As the name suggest, FastMM is the Fastest memory leak reporter. The memory leaks in the projects are reported once you close the running application. FastMM is mostly used and largely suggested tool for detecting Memory leak. Below are the steps described to integrate FastMM in your Delphi Application.
Steps to integrate FastMM in your Delphi application:>- Download the FastMM code from Github
- Unzip the downloaded zip file to a folder on your computer
- Go to Tools-> Options->Library and include the path to the downloaded files in the Library Path
- Right Click on your Project ->Source. Add ‘FastMM4’ in the uses section (FastMM4 should be added as the first unit)
- Go to the folder which is downloaded and edit FastMM4Options.inc file and enable FullDebugMode option. (To enable FulldebugMode, remove the dot from this {.$define FullDebugMode}).
Instead of manually editing the FastMM4Options.inc file, you can use The JED Software which is a GUI tool to edit FastMM4Options.inc file and configure FastMM - Copy the FastMM_FullDebugMode.dll ({Your Path}\FastMM4-master\FullDebugMode DLL\Precompiled) in your project bin folder
- Lastly, Go to Projects->Options and set ‘Map File’ property to ‘Detailed’
If any memory leak is encountered, FastMM will show a Memory leak message and an errotlog file will be generated at the bin folder of your application