Search code examples
delphimemory-managementfastmm

FastMM: Total Allocated Memory


How could I get the total amount of memory, that allocated by FastMM?

I've tried that:

function GetTotalAllocatedMemory: Cardinal;
var
  MMState: TMemoryManagerState;
begin
  GetMemoryManagerState(MMState);
  Result := MMState.TotalAllocatedMediumBlockSize + MMState.TotalAllocatedLargeBlockSize;
end;

Is it correct?

Anyways it returns something strange. It 5 times less than a value which I can see in Windows task manager. I believe that the amount of memory allocated by a Delphi application equals FastMM allocated memory plus some system overhead. Am I wrong?


Solution

  • Use this:

    //------------------------------------------------------------------------------  
    // CsiGetApplicationMemory  
    //  
    // Returns the amount of memory used by the application (does not include  
    // reserved memory)  
    //------------------------------------------------------------------------------  
    function CsiGetApplicationMemory: Int64;  
    var  
      lMemoryState: TMemoryManagerState;  
      lIndex: Integer;  
    begin  
      Result := 0;  
    
      // get the state  
      GetMemoryManagerState(lMemoryState);  
    
      with lMemoryState do begin  
        // small blocks  
        for lIndex := Low(SmallBlockTypeStates) to High(SmallBlockTypeStates) do  
          Inc(Result,  
              SmallBlockTypeStates[lIndex].AllocatedBlockCount *  
              SmallBlockTypeStates[lIndex].UseableBlockSize);  
    
        // medium blocks  
        Inc(Result, TotalAllocatedMediumBlockSize);  
    
        // large blocks  
        Inc(Result, TotalAllocatedLargeBlockSize);  
      end;  
    end;