The new Memory Manager in Delphi

Yesterday evening I attended a Delphi user group meeting in Stockholm, Sweden.

Memory Manager

I spoke very shortly about the change of Memory Manager in Delphi 2006. Except for the IDE, the new memory manager only affects Delphi and C++ applications compiled to native code (i.e. Win 32) not .Net applications.

function GetMem(Size: Integer): Pointer;

function FreeMem(P: Pointer): Integer;

function ReallocMem(P: Pointer; Size: Integer): Pointer;

The Memory Management is only three functions to implement as effectively as possible. These basal functions are used by almost everything in you application so changing them can have a remarkable effect on your application – they affect the overall speed and memory usage.

But implementing them is easier said than done. There is a lot of interesting optimizations problems; memory fragmentation is one area.

Memory Fragmentation

The large rectangle in picture 1 represents one part of your heap. It is a larger memory block that the memory manager required from the operating system. The green rectangles are smaller blocks that are in use – they could contain for example a string, a button instance, a dynamic array, etc.

In picture 2 more objects are created and space is reserved for them on the heap. In picture 3 old objects are freed and memory is available for use again.

Notice that we get gaps. In picture 4 we need to allocate room for a new block that is slightly larger. We need to spend time to search for a space that is large enough and when we find a gap the fit might not be perfect and we get smaller gaps of unused space.


FastCode Project

In 2005 the FastCode Project had a challenge to write the fastest replacement for the Delphi Memory Manager. The Fastcode Challenges is a project running on a volunteer basis that provides highly optimized functions for the Delphi community. (Thank you guys!) Functions are faster versions of Delphi runtime library functions, VCL functions or functions meant as extensions for these.

Pierre le Riche won the challenge with his memory manager FastMM. In Developer Studio
2006 (and the new Turbo Delphi and Turbo C++) is the old Memory Manager (located in replaced with Pierre’s code from FastMM.

New and Old

The FastCode project has a benchmark and validation tool to measure and test the different contributions and the benchmark below compares the default memory manager in Delphi 7 and Delphi 2006.


As you can see there is a drastic difference. Will it double the speed of your application if you recompile? Probably not but it depends on your code off course. I guess the easiest way to find out is to recompile your application in Delphi 2006 and test.

There are other benefits except for increased speed and reduced memory consumption. Since there is less memory fragmentation, memory intense application can run longer before the dreaded out of memory exception occurs. You can also use more than 3GB memory.

Report on Memory Leaks

The new memory manager works differently than the old memory manager and does more pointer checking, so it will catch more errors.

Setting ReportMemoryLeaksOnShutdown to True shows a message box with a report on all memory leaks every time you shut down your application.

{Display leaks on shutdown if a debugger is present}
ReportMemoryLeaksOnShutdown := DebugHook <> 0;

Turning it on can be a bit horrifying if you add it to an existing project. It will take some time before you get a clean project but it can be very useful if you have it in place from the beginning of a new project.

Read more about the new memory manager in a BDN article by Pierre le Riche.

Stuck on older versions?

It is possible to replace the default memory manager in older Delphi versions with your own. The only thing you have to do is to call SetMemoryManager in the System unit. If you have an old Delphi application you have not upgraded yet it is possible to download FastMM and use it in your existing application.