2012-03-09 107 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

相關問題