What are memory leaks and what are their consequences?

A memory leak happens when your application allocates a space in memory and never frees it again.

How memory leaks happen in Delphi

In a Delphi application, memory spaces are allocated and released all the time. This is often done automatically by the compiler or by the RTL – for example, when allocating variables of primitive types, parameters that are passed to functions, etc. – and we usually do not have to worry about it.

However, there are many cases where we allocate memory manually. Instantiating an object is one of them:

Obj: = TMyObject.Create;

The code above will allocate a memory space and the Obj variable will point to the address of allocated memory space. To release it, you can use the Free method:


If the developer forgets to call the Free method, the memory space associated with the object is never released. We have a memory leak.

Consequences of memory leaks

You may wonder: "What’s the problem? I’ve seen this happen in my application and I’ve never had any practical problems!"

Actually, we often don’t see issues when there are just a few memory leaks, and the consequences also depend on the type of applications:

Client applications (desktop or mobile)

In general, in desktop or mobile applications, the consequences are not very serious. In current operating systems, the memory allocated by the application is generally released when the application is terminated, so as not to cause system instability.

Even so, if your application generates a lot of memory leaks, there may be some problems, caused by excessive memory usage:

  • Slow application and/or system: Disk access may be necessary to get around the memory shortage.

  • Application abrupt closing: The operating system may force application termination due to excessive memory usage. This is more common in mobile applications.

  • Application bugs: Application code that allocates memory can start to crash and cause bugs.

Server Applications

When talking about server applications, the problem gets worse. That’s because ideally you will never close the server – you want it to run "forever" without needing to restart it.

The consequences are almost the same as the ones for desktop applications, but since the server application theoretically should never close, any small memory leak will have consequences in the future. The memory will slowly be consumed over days, weeks, months, and your server performance will degrade, bugs will appear and eventually the server will stop working.

Avoiding Memory Leaks

There are techniques and tools to help you detect and remove memory leaks from your application. This will be covered in future posts.

What about you? Have you ever had issues in your application caused by memory leaks? Do you consider detecting and removing memory leaks a low-priority task compared to other more important things to worry about in your code? Leave a comment!