我正在使用Delphi 2009编码,想知道程序使用了多少内存。由于内存管理器在对象被释放时不会将未使用的内存释放回操作系统,而是可能缓存在内存管理器中以备下次使用。我的问题是是否有可能知道程序使用了多少内存,但要排除缓存在内存管理器中的内存。谢谢。
我正在使用Delphi 2009编码,想知道程序使用了多少内存。由于内存管理器在对象被释放时不会将未使用的内存释放回操作系统,而是可能缓存在内存管理器中以备下次使用。我的问题是是否有可能知道程序使用了多少内存,但要排除缓存在内存管理器中的内存。谢谢。
我有一个例程,在调试模式下调用FastMM函数以获取内存使用情况(正如David建议的那样)。当FastMM没有安装时,即在发布模式下,我使用以下代码,只需要引用Delphi的System单元:
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。