How to measure memory usage

I am coding Delphi 2009, I want to know how much memory is used by the program. since the memory manager does not release unused memory back to the OS when the object is freed, it can cache memory for later use. My question is, is there a way to find out how much memory the program is using. It should exclude memory caching in the memory manager. Thanks.

+4
source share
1 answer

I have a routine that, in debug mode, calls the FastMM function to use memory (as David suggested). When FastMM is not installed, that is, in my release mode, I use the following code, which requires only a reference to the Delphi system unit:

function GetAllocatedMemoryBytes_NativeMemoryManager : NativeUInt; // Get the size of all allocations from the memory manager var MemoryManagerState: TMemoryManagerState; SmallBlockState: TSmallBlockTypeState; i: Integer; begin GetMemoryManagerState( MemoryManagerState ); Result := 0; for i := low(MemoryManagerState.SmallBlockTypeStates) to high(MemoryManagerState.SmallBlockTypeStates) do begin SmallBlockState := MemoryManagerState.SmallBlockTypeStates[i]; Inc(Result, SmallBlockState.AllocatedBlockCount*SmallBlockState.UseableBlockSize); end; Inc(Result, MemoryManagerState.TotalAllocatedMediumBlockSize); Inc(Result, MemoryManagerState.TotalAllocatedLargeBlockSize); end; 

I am using XE2, so you may need to change NativeUInt to Int64.

+1
source

Source: https://habr.com/ru/post/1400536/


All Articles