我正在使用Delphi 2009进行编码,我想知道程序使用了多少内存。由于当对象被释放时,内存管理器不会将未使用的内存释放回操作系统,因此它可能会在内存管理中缓存以备下次使用。我的问题是,是否有可能知道程序使用了多少内存。它应该排除缓存在内存管理器中的内存。谢谢
我有一个例程,它在调试模式下调用FastMM函数来使用内存(正如David所建议的)。当没有安装FastMM时,即在我的发布模式下,我使用以下代码,只需要参考Delphi的系统单元:
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;
我使用XE2,所以您可能需要将NativeUInt更改为Int64。