1

If an TObject is created, it must be destroyed on the end. In the process of developing it happened to me that the running application was reacting slowly after a short time, and I noticed in the Windows Task Manager that it consumed huge amounts of memory after it was running for a while. I have made some changes, but still I see little progressive memory consumption. The app has about 22,000 Kb in memory and after repetitive redrawing, which involves creating and destroying of objects it rises to 33,000.

Is this a clear sign that somewhere in the code I do not free objects correctly or are there other causes of the memory leak? Is there a method for debugging this problem easily?

I always pay great attention to create and destroy objects in the right way, so I do not see an obvious failure.

karel
  • 5,489
  • 46
  • 45
  • 50
lyborko
  • 2,571
  • 3
  • 26
  • 54
  • 1
    Impossible to say from this whether you have a leak or not. Use FastMM in full debug more to detect leaks. – David Heffernan Dec 11 '17 at 00:43
  • 1
    It's not a sign of memory leaks at all. Memory manager can just keep unused memory blocks for later use. So even if you release all the used objects, there can be memory blocks which memory manager plans to use after. – Victoria Dec 11 '17 at 01:15
  • 1
    Suggest reviewing: https://stackoverflow.com/questions/416046/what-is-the-best-tool-to-detect-memory-leaks-in-delphi?rq=1 – Darian Miller Dec 11 '17 at 01:50

2 Answers2

5

Memory leaks can be reported on shutdown in debug mode by adding this to your project source code:

{$WARN SYMBOL_PLATFORM OFF}
ReportMemoryLeaksOnShutdown := DebugHook <> 0;

Go Project > View Source to find this and place the above line before Application.Initialize. It should look something like this:

  begin
    {$WARN SYMBOL_PLATFORM OFF}
    ReportMemoryLeaksOnShutdown := DebugHook <> 0;
    {$WARN SYMBOL_PLATFORM ON}
    Application.Initialize;
    Application.MainFormOnTaskbar := True;
    Application.CreateForm(TfrmMain, frmMain);
    Application.Run;
  end.
Kim Somers
  • 123
  • 2
  • 12
  • Hmm, I've never seen this using `DebugHook`... I typically just use a conditional `{$IFDEF DEBUG} ... ` – Jerry Dodge Dec 11 '17 at 09:54
  • It's a very handy tool within the IDE and specifically used in the older versions without the need of bringing in open source or third-party tools. – Kim Somers Dec 11 '17 at 10:06
  • I mean I know about `ReportMemoryLeaksOnShutdown`, but not the `DebugHook <> 0` part. – Jerry Dodge Dec 11 '17 at 10:08
  • Right, assigning it to the DebugHook <> 0 means it only reports during debugging. – Kim Somers Dec 11 '17 at 10:21
  • 2
    Combine this with using FastMM4 or LeakCheck and you get useful information about allocation callstack and stuff and not just "there has been some leak of 15 x TFoo and 4 x TBar" as you get with just turning `ReportMemoryLeaksOnShutdown` on. – Stefan Glienke Dec 11 '17 at 11:20
4

The best way to check for memory leaks is to use a tool that checks for memory leaks. Task Manager only reports memory usage and memory use can increase for legitimate reasons, not just memory leaks.

Further, Delphi application memory is managed by the Delphi memory manager. The way that Delphi memory management works can give the impression of a memory leak due to the way that unused memory is reclaimed from the system (i.e. usually only when necessary, since re-cycling unused memory is more efficient than having to go back to the operating system to re-allocate memory that had been returned to the OS).

Memory fragmentation further complicates the issue when it comes to detecting memory leaks simply by monitoring memory usage.

One of the simplest ways to check for memory leaks is to enable the capabilities of the Delphi memory manager in this area. This became easier when Delphi adopted the FastMM memory manager as the default as of Delphi 2006. However, this FastMM memory manager (open source) can also be used explicitly and very easily as a replacement memory manager in earlier Delphi versions.

If you are using Delphi 2006 or later, some starter information on using the new default memory manager debugging capabilities can be found in the Delphi documentation.

For use of the open source version with earlier versions of Delphi there are numerous blog posts on the web, of which this is just one.

Deltics
  • 22,162
  • 2
  • 42
  • 70