Extensive Use Of Stringlists Causing Huge Memory Allocations.

The easier way to fix the problem is to let Windows handle all the memory for you since it doesn't keep a heap full of holes in allocated memory. Here's a very simple alternate memory manager unit. Just insert it as the first unit in the uses statement in the .DPR file and the problem goes away.

NOTE: you don't need to do this with Delphi 2006 as it uses the Fastmm memory manager by default and it does not have the memory fragmentation problems prior versions of Delphi had. You can also get Fastmm memory manager for older versions of delphi here: http://sourceforge.net/projects/fastmm/ If you are using a threaded Synapse server you should be using FastMM for best results.

unit MemoryManager;
uses SysUtils;
type EMemoryManager = class(Exception);
uses Windows;
function MGetMem(Size : Integer) : Pointer;
  if Size=0 then
    raise EMemoryManager.Create
      ('Why did you pass MGetMem Size=0?  You know I can''t do that.');
  Result:=GlobalAllocPtr(GMEM_FIXED, Size);
  if Result=nil then
    raise EMemoryManager.CreateFmt
      ('MGetMem failed while trying to allocate %d bytes, error %d', [Size, GetLastError]);
function MFreeMem(P : Pointer) : Integer;
  if Result0 then
    raise EMemoryManager.CreateFmt
      ('MFreeMem failed while trying to free %p, error %d', [P, GetLastError]);
function MReallocMem(P : Pointer; Size : Integer) : Pointer;
  Result:=GlobalReallocPtr(P, Size, GMEM_MOVEABLE);
  if Result=nil then 
    raise EMemoryManager.CreateFmt
      ('MReallocMem failed while trying to reallocate %p from %d to %d bytes, error %d',
      [P, GlobalSize(HGLOBAL(P)), Size, GetLastError]);
const NewMemoryManager : TMemoryManager = (GetMem: MGetMem; FreeMem: MFreeMem; ReallocMem: MReallocMem);
public/howto/memorymanager.txt · Last modified: 2007/11/30 14:30 (external edit)
Driven by DokuWiki Recent changes RSS feed