//取得指定進程占用內(nèi)存
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;
//取得當前進程占用內(nèi)存
function CurrentMemoryUsage: Cardinal;
var
pmc: TProcessMemoryCounters;
begin
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.WorkingSetSize
else
RaiseLastOSError;
end;
//調(diào)用上面兩個函數(shù),返回當前進程占用內(nèi)存
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;