• Blog
  • How to catch memory leaks in Delphi apps

How to catch memory leaks in Delphi apps

What Memory Leaks are and how to handle them in Delphi apps

Publish date:
Discover more of what matters to you

Overview 

In this article, we are going to explain what Memory Leaks are and how to handle them in Delphi applications. The article is intended for Delphi developers who are looking for some reference on memory leaks. We will consider different third-party components (free as well as commercial) that are available to handle memory leaks in Delphi applications.

Most of the modern Object oriented languages require you to create an instance of a class before you start using it. A variable of a class type that we define does not contain the value of the object. It is just a reference or pointer to a memory location where the object will be stored in memory.

What is a memory leak?

Delphi is based on the Object reference Model. And using it, you have to create an object for the classes manually.

Objects can be created by calling constructors of the class. Once you have created an object, memory is allocated for it to use. When you do not need an object anymore, you have to free it manually to avoid a memory leak. 

Difference between Free and FreeAndNil:

Free deallocates the memory allocated for an object. FreeAndNil first resets the object reference to nil and then frees it. FreeAndNil can be used when you want to initialize an object just before its use and free it after its use. This concept is called “Lazy creation” and it is commonly applied by Delphi programmers.

Example of FreeAndNil

123456789101112
procedure TForm1.btnMemoryLeakClick(Sender: TObject);
var
lstringlist: tstringlist;
begin
lstringlist := tstringlist.Create;
try
lstringlist.Add('Test String');
finally
FreeAndNil(lstringlist);
// lstringlist.Free;
end;
end;

Garbage collection:

Such languages as C# and Java automatically free the object when it is no longer referenced by the program. This behavior is known as Garbage collection. Garbage collection reduces the programmers’ work of explicitly freeing the memory but at the expense of performance downgrade.

Delphi does not support Garbage collection and leaves the responsibility of freeing objects to programmers. As a result,  programs written in Delphi are super efficient in comparison with the ones written in other languages. Delphi programmers have to be extra cautious to be free of the resources that are being used during the program execution time.

If you do not free the memory being used by the program, quite soon, you will start observing lags in performance, applications going into a not responding state, etc. Due to this, memory resources should be carefully handled especially inside loops, threads, etc.

Delphi has many powerful third-party tools and libraries that can help in detecting memory leaks. Let’s have a look at some of them.

FastMM:

FastMM is a widely used tool for detecting memory leaks. The memory leaks in the projects are reported once you close the running application.

Below you can find the steps which you can follow to integrate FastMM into your Delphi Application:

  • Download FastMM (https://github.com/pleriche/FastMM4)
  • Unzip the archive to any desired folder.
  • Add library path pointing to FastMM library (Go to Tools-> Options->Library and include the path to the downloaded files to the Library Path)
FastMM
  • FastMM4.pas should be added as the first unit in the project source. Add FastMM.pass to your project Right Click on your Project ->Source. Add ‘FastMM4’ to the Uses section
FastMM4
  • FastMM4.pas should be added as the first unit to the project source. To change the order to first, right-click on your Project ->Source and move fast report pas file reference to the first position in the uses clause.
FastMM4.pas
FastMM4.pas
  • Go to the FastMM directory, edit the FastMM4Options.inc file, and enable the FullDebugMode option. To enable FulldebugMode, remove the dot from this {.$define FullDebugMode} line.
  • Copy the FastMM_FullDebugMode.dll ({Your FastMM directory Path}\FastMM4-master\FullDebugMode DLL\Precompiled) in your project bin folder.
  • Now you need to set  Project’s ‘Map File’ property to ‘Detailed’
    Go to Projects->Options and set the ‘Map File’ property to ‘Detailed’
Map File
  • If any memory leak is encountered during project execution, FastMM will show a Memory leak message and an error log file will be generated in the bin folder of your application

EurekaLog:

EurekaLog is the exception tracing tool that can give your application the ability to catch bugs and generates a detailed log with the call stack information like a  unit name, class name, method, and line number. This information is also logged into a file. Also, this tool provides the ability for end users of the application to send the information about these error logs to the application developer via email.Apart from all these capabilities, EurekaLog also provides fine memory leak detection support. To enable this feature, you need to check the “Catch memory leaks” option on the “Memory problems” tab.

EurekaLog

madExcept:

MadExcept is a commercial library that was developed to help developers locate bugs in their software. Whenever there is any crash or exception, madExcept will automatically catch it, log lots of useful information, and provide end users with the possibility to send developers a full bug report. madExcept is also able to find memory leaks, resource leaks, and buffer overruns.

madExcept settings config page:

madExcept

We have reviewed a couple of third-party options available to detect/report memory leaks. Apart from this, there are other alternatives like deleaker, LeakCheck, AQTime Pro, Nexus Quality Suite, DDDebug, etc. Some of them are free and some are commercial ones. Some are Windows-platform specific and some are cross-platform. 

In this article, we have summarized the key information about the memory leak issue, the root causes of the memory leak, and explained how to free the instantiated objects and memory resources. The amount of information provided in this article is considerable but it is just the tip of the iceberg. And it may be required to dive deeper into the subject. You can contact Softacom for getting a reliable and hassle-free solution for any memory leak or laggy application issue. We will also be delighted to help you with our knowledge and rich expertise in Delphi migration.

Subscribe to our newsletter and get amazing content right in your inbox.

This field is required
This field is required Invalid email address

Thank you for subscribing!
See you soon... in your inbox!

confirm your subscription, make sure to check your promotions/spam folder

Tags

Subscribe to our newsletter and get amazing content right in your inbox.

You can unsubscribe from the newsletter at any time

This field is required
This field is required Invalid email address

You're almost there...

A confirmation was sent to your email

confirm your subscription, make sure to check
your promotions/spam folder