I am coding with Delphi 2009, I want to know how much memory the program used. since the memory manager doesn't releases unused memory back to OS when the object are freed, it might cache in memory manage for next use. My question is if there is a possible way to known how much memory the program used. It should exclude the memory cached in memory manager. Thanks.
Asked
Active
Viewed 1,476 times
4
-
1If I recall correctly, the full version of FastMM contains a demo memory usage tracker program. That sounds like what you need. – David Heffernan Mar 09 '12 at 08:23
-
2I think there are several questions on this topic already. See for exampel http://stackoverflow.com/questions/4448129/why-doesnt-my-programs-memory-usage-return-to-normal-after-i-free-memory or http://stackoverflow.com/questions/4475592/how-to-convince-the-memory-manager-to-release-unused-memory – jpfollenius Mar 09 '12 at 08:53
-
Could somebody comment "Inside - Windows" value in http://stackoverflow.com/questions/4448129/why-doesnt-my-programs-memory-usage-return-to-normal-after-i-free-memory question – Branko Mar 09 '12 at 10:48
-
Nobody knows what you mean, Branko. – Warren P Mar 09 '12 at 21:46
-
Ok. In this example - GetMem(P, 1024 * 1024) - FastMem show memory usage 1.048.768 B, GetProcessMemoryInfo() only 4.096 ? – Branko Mar 10 '12 at 09:45
-
I thought that using a Windows function to get the memory use was not so useful in that you only get cached values. It's much better to query the Delphi memory manager. – Brian Frost Mar 12 '12 at 17:38
1 Answers
1
I have a routine that in debug mode calls the FastMM function to get memory use (as David suggested). When FastMM is not installed i.e. in my release mode I use the following code which only needs a reference to Delphi's 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 use XE2 so you might have to change NativeUInt to Int64.

Brian Frost
- 13,334
- 11
- 80
- 154
-
NativeInt exists on delphi 2009 although I would expect it to be NativeUInt for 32 bit processes that allocate more than 2GB memory. – David Heffernan Mar 10 '12 at 07:37