Differences

This shows you the differences between two versions of the page.

Link to this comparison view

public:howto:memorymanager [2007/11/30 14:30] (current)
Line 1: Line 1:
 +====== 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.
 +
 +<code delphi>
 +[MemoryManager.pas]
 +unit MemoryManager;
 +
 +interface
 +
 +uses SysUtils;
 +
 +type EMemoryManager = class(Exception);
 +
 +implementation
 +
 +uses Windows;
 +
 +function MGetMem(Size : Integer) : Pointer;
 +begin
 +  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]);
 +end;
 +
 +function MFreeMem(P : Pointer) : Integer;
 +begin
 +  Result:=GlobalFreePtr(P);
 +  if Result0 then
 +    raise EMemoryManager.CreateFmt
 +      ('MFreeMem failed while trying to free %p, error %d', [P, GetLastError]);
 +end;
 +
 +function MReallocMem(P : Pointer; Size : Integer) : Pointer;
 +begin
 +  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]);
 +end;
 +
 +const NewMemoryManager : TMemoryManager = (GetMem: MGetMem; FreeMem: MFreeMem; ReallocMem: MReallocMem);
 +
 +initialization
 +  SetMemoryManager(NewMemoryManager);
 +end.
 +</code>
public/howto/memorymanager.txt · Last modified: 2007/11/30 14:30 (external edit)
Driven by DokuWiki Recent changes RSS feed