如何获取Delphi程序使用的内存

53

我知道如何使用GlobalMemoryStatusEx获取系统内存使用情况,但它只告诉我整个操作系统正在使用的内存。

我真的希望我的程序报告它单独分配和使用了多少内存。

在我的Delphi 2009程序中,是否有任何方式调用Windows函数或某些FastMM功能,以查找仅由我的程序分配的内存?


重新审视我的问题,我现在已将我的接受答案更改为@apenwarr的GetMemoryManagerState答案。 它生成与我曾经使用的GetHeapStatus函数(现已弃用)相同的结果,而GetProcessMemoryInfo.WorkingSetSize给出了非常不同的结果。

6个回答

83

你可以在不使用任何直接的Win32调用的情况下,从Delphi运行时获得有用的内存使用信息:

unit X;

uses  FastMM4; //include this or method will return 0.
....

function GetMemoryUsed: UInt64;
var
  st: TMemoryManagerState;
  sb: TSmallBlockTypeState;
begin
  GetMemoryManagerState(st);
  result :=  st.TotalAllocatedMediumBlockSize
           + st.TotalAllocatedLargeBlockSize;
  for sb in st.SmallBlockTypeStates do begin
    result := result + sb.UseableBlockSize * sb.AllocatedBlockCount;
  end;
end;

这种方法最好的一点是它被严格地跟踪:当你分配内存时,它会增加,而当你释放内存时,它会立即减少相同数量。我在每次运行单元测试之前和之后使用它,这样我就可以知道哪个测试正在泄漏内存(例如)。


7
如果您使用的是FastMM4或Delphi 2006+,则此方法有效;但是,如果您没有使用FastMM4,则应考虑开始使用它! - Khalid Salomão
7
这可能是我在StackOverflow上看过的最好和最有用的答案。我希望我能给它点赞100次。 - Nick Hodges
1
除此之外,该方法显示应用程序分配的内存量,而不是其使用的内存量(例如由第三方dll、ocx/COM分配的内存等)。为此,Jim McKeeth在下面提供了一个更可靠的解决方案,前提是将MemCounters.PagefileUsage添加到结果中。 - ciuly
5
小提示:您需要在实现此内容的单元中使用(将其设置在uses部分)FastMM4。仅在项目单元中添加FastMM4是不够的。 - Roeland Van Heddegem
1
@Makla 只需在实现此功能的单元中的 uses 部分添加 "FastMM4" 即可。我无法更好地解释了... - Roeland Van Heddegem
显示剩余7条评论

28

从我以前的博客文章中获取

想要知道程序正在使用多少内存吗?这个 Delphi 函数可以解决问题。

uses psAPI;

{...}

function CurrentProcessMemory: Cardinal;
var
  MemCounters: TProcessMemoryCounters;
begin
  MemCounters.cb := SizeOf(MemCounters);
  if GetProcessMemoryInfo(GetCurrentProcess,
      @MemCounters,
      SizeOf(MemCounters)) then
    Result := MemCounters.WorkingSetSize
  else
    RaiseLastOSError;
end;

我不确定我是从哪里学到这个基础知识的,但我添加了一些更好的错误处理,并将其制作成了一个函数。WorkingSetSize是当前使用的内存量。您可以使用类似的代码来获取当前进程(或任何进程)的其他值。您需要在uses语句中包含psAPI。

PROCESS_MEMORY_COUNTERS记录包括:

  • PageFaultCount
  • PeakWorkingSetSize
  • WorkingSetSize
  • QuotaPeakPagedPoolUsage
  • QuotaPagedPoolUsage
  • QuotaPeakNonPagedPoolUsage
  • QuotaNonPagedPoolUsage
  • PagefileUsage
  • PeakPagefileUsage

您可以在任务管理器或进程资源管理器中找到所有这些值。


这个函数的输出数字不断增长,已经显示使用了7000064字节的RAM,但任务管理器显示该进程仅使用了1972 kb的RAM。顺便说一句,你博客的链接挂了。 - Paul
函数结果应该是NativeUInt而不是Cardinal,这样在编译成64位应用程序时就可以得到64位的结果了,不是吗? - kbriggs
那可能是32位代码,但对于64位,您需要使用NativeUInt。 - Jim McKeeth

4
您可以通过从SourceForge下载完整的FastMM4捆绑包并使用其中包含的示例项目UsageTrackerDemo来了解如何使用FastMM。请注意,保留HTML标记,但是我已经将其翻译为中文。

4
我写了一个小函数来返回当前进程(应用程序)的内存使用情况:
function ProcessMemory: longint;
var
  pmc: PPROCESS_MEMORY_COUNTERS;
  cb: Integer;
begin
  // Get the used memory for the current process
  cb := SizeOf(TProcessMemoryCounters);
  GetMem(pmc, cb);
  pmc^.cb := cb;
  if GetProcessMemoryInfo(GetCurrentProcess(), pmc, cb) then
     Result:= Longint(pmc^.WorkingSetSize);

  FreeMem(pmc);
end;

2
为什么要使用GetMem?直接在那里声明一个TProcessmemoryCounters变量,而不是使用动态变量。 - Rob Kennedy
这是答案,带有Rob的优化。 - lkessler
2
这取决于你认为的内存使用是什么。这段代码给出了工作集大小,也就是任务管理器所称的内存使用情况。但它远远不是进程正在使用的内存量。它只是当前在RAM中而不是页面文件中的部分。 - Lars Truijens

2

Gant编写的C++代码转换为Delphi控制台应用程序:

    program MemoryProcessCMD;

    {* Based in Gant(https://stackoverflow.com/users/12460/gant) code*}


    {$APPTYPE CONSOLE}
    {$R *.res}

    uses
      System.SysUtils,
      psapi,
      Windows;

    procedure PrintMemoryInfo(processID: DWORD);
    var
      hProcess: THandle;
      pmc: PROCESS_MEMORY_COUNTERS;
      total: DWORD;

    begin

      // Print the process identifier.
      Writeln(format('Process ID: %d', [processID]));

      // Print information about the memory usage of the process.
      hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE,
        processID);

      if (hProcess = 0) then
      begin
        exit;
      end;

      if (GetProcessMemoryInfo(hProcess, @pmc, SizeOf(pmc))) then
      begin
        Writeln(format(#09'PageFaultCount: 0x%.8X', [pmc.PageFaultCount]));
        Writeln(format(#09'PeakWorkingSetSize: 0x%.8X', [pmc.PeakWorkingSetSize]));
        Writeln(format(#09'WorkingSetSize: 0x%.8X', [pmc.WorkingSetSize]));
        Writeln(format(#09'QuotaPeakPagedPoolUsage: 0x%.8X',
          [pmc.QuotaPeakPagedPoolUsage]));
        Writeln(format(#09'QuotaPagedPoolUsage: 0x%.8X',
          [pmc.QuotaPagedPoolUsage]));
        Writeln(format(#09'QuotaPeakNonPagedPoolUsage: 0x%.8X',
          [pmc.QuotaPeakNonPagedPoolUsage]));
        Writeln(format(#09'QuotaNonPagedPoolUsage: 0x%.8X',
          [pmc.QuotaNonPagedPoolUsage]));
        Writeln(format(#09'PagefileUsage: 0x%.8X', [pmc.PagefileUsage]));
        Writeln(format(#09'PeakPagefileUsage: 0x%.8X', [pmc.PeakPagefileUsage]));
        Writeln(format(#09'PagefileUsage: 0x%.8X', [pmc.PagefileUsage]));
      end;

      CloseHandle(hProcess);
    end;

    var
      aProcesses: array [0 .. 1024] of DWORD;
      cbNeeded, cProcesses: DWORD;
      i: Integer;

    begin
      try
        // Get the list of process identifiers.
        if (not EnumProcesses(@aProcesses, SizeOf(aProcesses), &cbNeeded)) then
          halt(1);

        // Calculate how many process identifiers were returned.
        cProcesses := cbNeeded div SizeOf(DWORD);

        // Print the memory usage for each process
        for i := 0 to cProcesses - 1 do
        begin
          PrintMemoryInfo(aProcesses[i]);
        end;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;

    end.

1

对于 Win32 API 的方式,您需要使用 GetProcessMemoryInfo 函数。这里有一个来自 MSDN 页面 的示例,但代码是用 C++ 编写的。我认为您也可以将其转换为 Delphi。您要查找的可能是“工作集大小”。

#include <windows.h>
#include <stdio.h>
#include <psapi.h>

void PrintMemoryInfo( DWORD processID )
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Print information about the memory usage of the process.

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
    if (NULL == hProcess)
        return;

    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
    {
        printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
        printf( "\tPeakWorkingSetSize: 0x%08X\n", 
                  pmc.PeakWorkingSetSize );
        printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
        printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakPagedPoolUsage );
        printf( "\tQuotaPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPagedPoolUsage );
        printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakNonPagedPoolUsage );
        printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaNonPagedPoolUsage );
        printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); 
        printf( "\tPeakPagefileUsage: 0x%08X\n", 
                  pmc.PeakPagefileUsage );
    }

    CloseHandle( hProcess );
}

int main( )
{
    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
        return 1;

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the memory usage for each process

    for ( i = 0; i < cProcesses; i++ )
        PrintMemoryInfo( aProcesses[i] );

    return 0;
}

2
谢谢您开始了这个话题。答案是正确的,但语言不对,而且太复杂了。 - lkessler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接