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

如何获取Delphi程序使用的内存,delphi,memory-management,fastmm,Delphi,Memory Management,Fastmm,我知道如何使用GlobalMemoryStatusEx获得系统内存使用,但这告诉我整个操作系统在使用什么 我真的想让我的程序报告它自己分配和使用了多少内存 在我的Delphi 2009程序中,是否有任何方法可以调用Windows函数或某个FastMM函数来查找仅由我的程序分配的内存 再次回顾我的问题,我现在已将已接受的答案更改为@apenwarr提供的getMemoryManager状态答案。它产生了与我以前使用的GetHeapStatus函数(现在已弃用)相同的结果,而GetProcessM

我知道如何使用GlobalMemoryStatusEx获得系统内存使用,但这告诉我整个操作系统在使用什么

我真的想让我的程序报告它自己分配和使用了多少内存

在我的Delphi 2009程序中,是否有任何方法可以调用Windows函数或某个FastMM函数来查找仅由我的程序分配的内存



再次回顾我的问题,我现在已将已接受的答案更改为@apenwarr提供的getMemoryManager状态答案。它产生了与我以前使用的GetHeapStatus函数(现在已弃用)相同的结果,而GetProcessMemoryInfo.WorkingSetSize给出了非常不同的结果

对于Win32 API方式,您需要GetProcessMemoryInfo函数。这里有一个例子,但是代码是C++的。我想你也可以把它转换成Delphi。您所看到的可能称为“工作集大小”

#包括
#包括
#包括
无效打印内存信息(DWORD processID)
{
处理hProcess;
进程\内存\计数器pmc;
//打印进程标识符。
printf(“\n进程ID:%u\n”,进程ID);
//打印有关进程内存使用情况的信息。
hproces=OpenProcess(进程\查询\信息|
进程_VM _READ,
FALSE,processID);
if(NULL==hProcess)
返回;
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.QUOTAPEAKNONPAGEDPOOLUSE);
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()
{
//获取进程标识符的列表。
DWORD APROCESS[1024],CBREQUIRED,CPROCESS;
无符号整数i;
if(!EnumProcesses(a进程、sizeof(a进程)和cbNeeded))
返回1;
//计算返回了多少进程标识符。
cprocess=cbNeeded/sizeof(DWORD);
//打印每个进程的内存使用情况
对于(i=0;i
当您从SourceForge下载完整的FastMM4捆绑包时,您可以查看一个示例,说明如何将FastMM与演示附带的UsageTrackerDemo项目一起使用。

我编写这个小函数是为了返回当前进程(应用程序)内存使用情况:

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;

想知道你的程序正在使用多少内存吗?这个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

进程\内存\计数器记录包括:

  • 页面错误计数
  • PeakWorkingSetSize
  • 工作设置大小
  • QuotaPeakPagedPoolUsage
  • QuotaPagedPoolUsage
  • QuotaPeakNonPagedPool用法
  • QuotaNonPagedPoolUsage
  • 页面文件使用
  • PeakPagefileUsage

您可以在任务管理器或Process Explorer中找到所有这些值

您可以从Delphi运行时获取有用的内存使用信息,而无需使用任何直接的Win32调用:

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;
这种方法最好的地方在于它是严格跟踪的:当你分配内存时,它会上升,当你释放内存时,它会立即下降相同的数量。在运行每个单元测试之前和之后,我都会使用此选项,因此我可以判断哪个测试正在泄漏内存(例如)。

将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.

为什么要使用GetMem?只需在此处声明一个TProcessmemoryCounters变量,而不是使用一个动态变量。这就是答案,通过Rob的优化。这取决于您认为的内存使用情况。此代码为您提供工作集大小,任务管理器称之为内存使用量。但这并不是进程正在使用的内存量。它是当前在RAM中的部分,而不是页面文件。感谢您启动它。正确的答案,但错误的语言和太复杂。这个方法在使用FASTMM4或Delphi 2006 +时是有效的,但是如果你不使用FASTMM4,你应该考虑开始使用它。这可能是我在StackOverflow上见过的最好、最有用的答案。我希望我能把它提高100次。小注:你必须在你实现这个的单元中使用(=在使用部分设置)FastMM4。仅在项目单元中添加FastMM4是不够的。@Makla只需在实现此功能的单元中的uses部分添加“FastMM4”。我无法解释得比这个更好…自从你9年前回答我的问题以来,我一直在我的程序中使用这个,但现在我切换到64位,这个数字太小了,超出了我的预期。果不其然,我想我已经找到了一个小小的修改,我现在已经对你的答案做了修改。
    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.