7

How could I get the total amount of memory, that allocated by FastMM?

I've tried that:

function GetTotalAllocatedMemory: Cardinal;
var
  MMState: TMemoryManagerState;
begin
  GetMemoryManagerState(MMState);
  Result := MMState.TotalAllocatedMediumBlockSize + MMState.TotalAllocatedLargeBlockSize;
end;

Is it correct?

Anyways it returns something strange. It 5 times less than a value which I can see in Windows task manager. I believe that the amount of memory allocated by a Delphi application equals FastMM allocated memory plus some system overhead. Am I wrong?

4

4 に答える 4

4

あなたはリンゴとオレンジを比較しています。

FastMM メモリは、FastMM によって割り当てられたメモリの正味の使用量です。

これには、少なくとも次のものは含まれません。

  • FastMM オーバーヘッド
  • ユーザーに代わって FastMM によって割り当てられたブロックの Windows オーバーヘッド
  • FastMM によって割り当てられていないものの Windows オーバーヘッド (プロセス空間で DLL が占有する空間など)
  • GUI アプリの場合: GDI、GDI+、DirectX、OpenGL、およびユーザーに代わって割り当てられたビジュアル オブジェクト用のその他のストレージのオーバーヘッド。

--jeroen

于 2011-03-29T10:09:32.353 に答える
4

これを使って:

//------------------------------------------------------------------------------  
// CsiGetApplicationMemory  
//  
// Returns the amount of memory used by the application (does not include  
// reserved memory)  
//------------------------------------------------------------------------------  
function CsiGetApplicationMemory: Int64;  
var  
  lMemoryState: TMemoryManagerState;  
  lIndex: Integer;  
begin  
  Result := 0;  

  // get the state  
  GetMemoryManagerState(lMemoryState);  

  with lMemoryState do begin  
    // small blocks  
    for lIndex := Low(SmallBlockTypeStates) to High(SmallBlockTypeStates) do  
      Inc(Result,  
          SmallBlockTypeStates[lIndex].AllocatedBlockCount *  
          SmallBlockTypeStates[lIndex].UseableBlockSize);  

    // medium blocks  
    Inc(Result, TotalAllocatedMediumBlockSize);  

    // large blocks  
    Inc(Result, TotalAllocatedLargeBlockSize);  
  end;  
end;
于 2011-03-29T10:40:10.373 に答える
3

プロセスメモリには、次を使用します。

//------------------------------------------------------------------------------
// CsiGetProcessMemory
//
// Return the amount of memory used by the process
//------------------------------------------------------------------------------
function CsiGetProcessMemory: Int64;
var
  lMemoryCounters: TProcessMemoryCounters;
  lSize: Integer;
begin
  lSize := SizeOf(lMemoryCounters);
  FillChar(lMemoryCounters, lSize, 0);
  if GetProcessMemoryInfo(CsiGetProcessHandle, @lMemoryCounters, lSize) then
    Result := lMemoryCounters.PageFileUsage
  else
    Result := 0;
end;
于 2011-03-29T10:43:52.963 に答える
3

私もこの状況に直面しました:

とにかく、それは何か奇妙なものを返します。これは、Windows タスク マネージャーで表示される値の 5 分の 1 です。Delphi アプリケーションによって割り当てられるメモリの量は、FastMM に割り当てられたメモリにシステム オーバーヘッドを加えたものに等しいと思います。私が間違っている?

そして、すべてのメモリがどこにあるかを見つけようとして数時間を無駄にしました。タスク マネージャーによると、私のアプリは 170 Mb を占めていましたが、FastMM の統計では、割り当てられたブロックの合計サイズが ~13 Mb でした。

12565K Allocated
160840K Overhead
7% Efficiency

(FastMMLogMemoryManagerStateToFileプロシージャの出力からの抜粋)。最後に、この膨大なオーバーヘッドは FullDebug モードが原因であることに気付きました。割り当てごとにスタックトレースを保持するため、多くの小さなメモリブロックが割り当てられている場合 (私のアプリには UnicodeString x 99137、不明 x 17014、および ~10000 の Xml オブジェクトがありました)、オーバーヘッドは恐ろしいものになります。FullDebug モードを削除すると、メモリ消費量が通常の値に戻りました。

これが誰かを助けることを願っています。

于 2015-04-29T16:09:48.457 に答える