Delphi XE7 取得进程占用内存的两个函数


使用单元:uses Psapi;

//取得指定进程占用内存

function GetProcessMemUse(PID: Cardinal): Cardinal;
var
 pmc: PPROCESS_MEMORY_COUNTERS; // uses psApi
 ProcHandle: HWND;
 iSize: DWORD;
begin
 Result := 0;
 iSize := SizeOf(_PROCESS_MEMORY_COUNTERS);
 GetMem(pmc, iSize);
 try
 pmc^.cb := iSize;
 ProcHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
 False, PID); // 由PID取得进程对象的句柄
 if GetProcessMemoryInfo(ProcHandle, pmc, iSize) then
 Result := pmc^.WorkingSetSize;
 finally
 FreeMem(pmc);
 end;
end;

//取得当前进程占用内存
function CurrentMemoryUsage: Cardinal;
var
 pmc: TProcessMemoryCounters;
begin
 pmc.cb := SizeOf(pmc) ;
 if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
 Result := pmc.WorkingSetSize
 else
 RaiseLastOSError;
end;
//调用上面两个函数,返回当前进程占用内存
function GetCurrentMemUse: Cardinal;
begin
//  Result := GetProcessMemUse(GetCurrentProcessId);
 Result:= CurrentMemoryUsage ;
end;

//显示
procedure TForm1.tmr1Timer(Sender: TObject);
begin
 stat1.Panels[0].Text := format('memory use: %d KB', [GetCurrentMemUse div 1024]);//1kb=1024b
end;

需要引用Winapi.PsAPI,win7 xe7测试通过    http://blog.sina.com.cn/s/blog_44fa172f0102vcp3.html
联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678