2012-03-09 111 views
4

我使用Delphi 2009进行编码,我想知道程序使用了多少内存。因为内存管理器在释放对象时不会将未使用的内存释放回操作系统,因此它可能会缓存在内存中以供下次使用。我的问题是如果有一种可能的方法来知道程序使用了多少内存。它应该排除在内存管理器中缓存的内存。谢谢。如何测量内存使用情况

+1

如果我没有记错,完整版的FastMM包含一个演示内存使用情况跟踪程序。这听起来像你所需要的。 – 2012-03-09 08:23:40

+2

我觉得这个话题有几个问题。参见exampel http://stackoverflow.com/questions/4448129/why-doesnt-my-programs-memory-usage-return-to-normal-after-i-free-memory或http://stackoverflow.com/questions/4475592/how-to-convince-memory-manager-release-unused-memory – jpfollenius 2012-03-09 08:53:29

+0

有人可以在http://stackoverflow.com/questions/4448129/why-doesnt-my中评论“Inside - Windows”的值-programs-memory-usage-return-to-normal-after-i-free-memory question – Branko 2012-03-09 10:48:45

回答

1

我有一个例程,在调试模式下调用FastMM函数来获取内存使用(如David建议)。当我在释放模式没有安装FastMM即我用下面的代码,只需要德尔福的系统单元的参考:

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的。

+0

NativeInt在delphi 2009上存在,但我认为它是NativeUInt,用于分配超过2GB内存的32位进程。 – 2012-03-10 07:37:41