How Much Memory Is Your Delphi Program Occupying? ... and more...
The TProcessMemoryCounters record wraps up the Windows
API PROCESS_MEMORY_COUNTERS structure. Here's the meaning of the other fields:
PageFaultCount - the number of page faults.
PeakWorkingSetSize - the peak working set size, in bytes.
WorkingSetSize - the current working set size, in bytes.
QuotaPeakPagedPoolUsage - The peak paged pool usage, in bytes.
QuotaPagedPoolUsage - The current paged pool usage, in bytes.
QuotaPeakNonPagedPoolUsage - The peak nonpaged pool usage, in bytes.
QuotaNonPagedPoolUsage - The current nonpaged pool usage, in bytes.
PagefileUsage - The current space allocated for the pagefile, in bytes.
Those pages may or may not be in memory.
PeakPagefileUsage - The peak space allocated for the pagefile, in bytes.
http://delphi.about.com/od/delphitip...mory_usage.htm
Delphi-Quellcode:
uses PsAPI
function CurrentMemoryUsage: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.WorkingSetSize
else
RaiseLastOSError;
end;
function PageFaultCount: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.PageFaultCount
else
RaiseLastOSError;
end;
function PeakWorkingSetSize: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.PeakWorkingSetSize
else
RaiseLastOSError;
end;
function WorkingSetSize: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.WorkingSetSize
else
RaiseLastOSError;
end;
function QuotaPeakPagedPoolUsage: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.QuotaPeakPagedPoolUsage
else
RaiseLastOSError;
end;
function QuotaPagedPoolUsage: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.QuotaPagedPoolUsage
else
RaiseLastOSError;
end;
function QuotaPeakNonPagedPoolUsage: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.QuotaPeakNonPagedPoolUsage
else
RaiseLastOSError;
end;
function QuotaNonPagedPoolUsage: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.QuotaNonPagedPoolUsage
else
RaiseLastOSError;
end;
function PagefileUsage: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.PagefileUsage
else
RaiseLastOSError;
end;
function PeakPagefileUsage: Cardinal;
var
pmc: TProcessMemoryCounters;
begin Result:=0;
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.PeakPagefileUsage
else
RaiseLastOSError;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Memo1.Clear;
Memo1.Lines.add(FormatFloat('Memory used: ,.# KB', CurrentMemoryUsage / 1024));
Memo1.Lines.add('PageFaultCount: '+ IntToStr(PageFaultCount));
Memo1.Lines.add(FormatFloat('PeakWorkingSetSize: ,.# KB', PeakWorkingSetSize / 1024));
Memo1.Lines.add(FormatFloat('WorkingSetSize: ,.# KB', WorkingSetSize / 1024));
Memo1.Lines.add(FormatFloat('QuotaPeakPagedPoolUsage: ,.# KB', QuotaPeakPagedPoolUsage / 1024));
Memo1.Lines.add(FormatFloat('QuotaPagedPoolUsage: ,.# KB', QuotaPagedPoolUsage / 1024));
Memo1.Lines.add(FormatFloat('QuotaPeakNonPagedPoolUsage: ,.# KB', QuotaPeakNonPagedPoolUsage / 1024));
Memo1.Lines.add(FormatFloat('QuotaNonPagedPoolUsage: ,.# KB', QuotaNonPagedPoolUsage / 1024));
Memo1.Lines.add(FormatFloat('PagefileUsage: ,.# KB', PagefileUsage / 1024));
Memo1.Lines.add(FormatFloat('PeakPagefileUsage: ,.# KB', PeakPagefileUsage / 1024));
end;