Archive for the ‘Delphi’ Category

Determining Allocated Memory in Delphi

Saturday, November 4th, 2006

One of the new features of BDS 2006 is a new memory manager. As a result, all of the old ways to get the amount of allocated memory have been deprecated. It is surprising to me that they did not give you an alternative way to get this information, but thanks to some searching of the FastMM code, here is a method to get the amount of allocated memory for your process.

function AllocatedMemory: Cardinal;
var
  MemoryManagerState: TMemoryManagerState;
  SmallBlockState: TSmallBlockTypeState;
begin
  GetMemoryManagerState(MemoryManagerState);
  Result := 0;
  for SmallBlockState in MemoryManagerState.SmallBlockTypeStates do
    Inc(Result, SmallBlockState.AllocatedBlockCount * SmallBlockState.UseableBlockSize);
  Inc(Result, MemoryManagerState.TotalAllocatedMediumBlockSize);
  Inc(Result, MemoryManagerState.TotalAllocatedLargeBlockSize);
end;