我正在尝试查找 Windows 7 嵌入式平板电脑上的内存泄漏。使用 Perfmon,我可以看到,当相关应用程序运行时,“已使用已提交字节数的百分比”以每小时 0.77% 的速度稳步增长。在具有相同条件的 Win XP(非嵌入式)系统上,它根本不增长。
我很好奇这个指标是如何计算的,以便将我自己的监控和日志记录代码添加到应用程序中。使用 GlobalMemoryStatus API 调用,我可以返回几个数字,但无论我怎么计算,都无法得到与 Perfmon 监控“已使用已提交字节数百分比”计数器时相同的百分比结果。
我如何获取这个参数?
更新
以下是 Delphi 的示例代码。只需创建一个表单,将备忘录和计时器放到其中即可。在 Win XP 下测试。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type TPerformanceInformation = packed record
cb : DWORD ;
CommitTotal : integer;
CommitLimit : integer ;
CommitPeak : Cardinal ;
PhysicalTotal : Cardinal ;
PhysicalAvailable : Cardinal ;
SystemCache : Cardinal ;
KernelTotal : Cardinal ;
KernelPaged : integer ;
KernelNonpaged : Cardinal ;
PageSize : Cardinal ;
HandleCount : DWORD ;
ProcessCount : DWORD ;
ThreadCount : DWORD ;
end ;
TpPerformanceInformation = ^TPerformanceInformation ;
type
TForm1 = class(TForm)
Timer1: TTimer;
Memo1: TMemo;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1 ;
function GetPerformanceInfo (
pPerformanceInformation : TpPerformanceInformation ;
cb : dword) : integer ; stdcall ; external 'psapi.dll' ;
implementation
{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
var
Status : TMemoryStatus;
PerformanceInfo : TPerformanceInformation ;
begin
Memo1.Clear ;
Status.dwLength := sizeof(TMemoryStatus) ;
GlobalMemoryStatus (Status) ;
Memo1.Lines.Add (Format ('Total memory used (%%) : %u',[Status.dwMemoryLoad])) ;
Memo1.Lines.Add (Format ('Physical memory used : %u',[(Status.dwTotalPhys - Status.dwAvailPhys)])) ;
Memo1.Lines.Add (Format ('Physical memory left : %u',[Status.dwAvailPhys])) ;
Memo1.Lines.Add (Format ('Total physical memory : %u',[Status.dwTotalPhys])) ;
Memo1.Lines.Add (Format ('Total physical used (%%): %1.2f',[100.0 * (Status.dwTotalPhys - Status.dwAvailPhys) / Status.dwTotalPhys])) ;
Memo1.Lines.Add (Format ('Total page file : %u',[Status.dwTotalPageFile])) ;
Memo1.Lines.Add (Format ('Page file used : %u',[(Status.dwTotalPageFile - Status.dwAvailPageFile)])) ;
Memo1.Lines.Add (Format ('Page file left : %u',[Status.dwAvailPageFile])) ;
Memo1.Lines.Add (Format ('Page file used (%%) : %1.2f',[100.0 * (Status.dwTotalPageFile - Status.dwAvailPageFile) / Status.dwTotalPageFile])) ;
if (GetPerformanceInfo (Addr (PerformanceInfo), Sizeof (TPerformanceInformation)) <> 0) then
begin
with PerformanceInfo do
begin
Memo1.Lines.Add (Format ('CommitTotal : %u',[CommitTotal])) ;
Memo1.Lines.Add (Format ('CommitLimit : %u',[CommitLimit])) ;
Memo1.Lines.Add (Format ('CommitPeak : %u',[CommitPeak])) ;
Memo1.Lines.Add (Format ('PhysicalTotal : %u',[PhysicalTotal])) ;
Memo1.Lines.Add (Format ('PhysicalAvailable: %u',[PhysicalAvailable])) ;
Memo1.Lines.Add (Format ('SystemCache : %u',[SystemCache])) ;
Memo1.Lines.Add (Format ('KernelTotal : %u',[KernelTotal])) ;
Memo1.Lines.Add (Format ('KernelPaged : %u',[KernelPaged])) ;
Memo1.Lines.Add (Format ('KernelNonpaged : %u',[KernelNonpaged])) ;
Memo1.Lines.Add (Format ('PageSize : %u',[PageSize])) ;
Memo1.Lines.Add (Format ('HandleCount : %u',[HandleCount])) ;
Memo1.Lines.Add (Format ('ProcessCount : %u',[ProcessCount])) ;
Memo1.Lines.Add (Format ('ThreadCount : %u',[ThreadCount])) ;
end ;
end ;
end;
end.
答案1
查看获取性能信息(). 它会退回提交号码。