How Much Memory Is Your Delphi Program Occupying?
Memory usage of Delphi programs in Task Manager is Wrong!?!
No matter how much you try to code without bugs and memory leaks .. the more complex the application the more possible is to leak some memory.If you want to know how much memory is used at a certain moment in the lifecycle of your application, you can "ask" the Task Manager to show you the memory used.
Since you "cannot" ask the user of the application to look at the Task Manager, a custom Delphi function will do the trick:
uses PsAPI ...
//current memory size of the current process in bytes
function CurrentMemoryUsage: Cardinal;
var
pmc: TProcessMemoryCounters;
begin
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
Result := pmc.WorkingSetSize
else
RaiseLastOSError;
end;
To display the memory usage in KB, use:
ShowMessage(FormatFloat('Memory used: ,.# K', CurrentMemoryUsage / 1024)) ;
Note: 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.
Memory in Task Manager
Here's a discussion on "Mem Usage" in Task Manager I've found on the groups (I think the original text was written by Peter Below):Task manager does not show the actual memory usage of the program, it shows you its "working set", which is a rather imprecise measure of the programs actual memory use, since it includes address ranges that have been only reserved but not yet committed. Task manager also has no idea whatsoever about the internal memory management done by the program it watches.
Practically all languages used to write programs, quite independent of the operating sytem they are trageted for, offer their own internal memory management to the programmer. These memory managers usually allocate large blocks from the OS and then subdivide these blocks as needed to fulfil the codes requests for memory allocations. The blocks do not get returned to the OS pool until every single byte of the block has been returned to the memory manager, so task manager will count the block as used even if only a single byte of it is still in use by the program.
No comments:
Post a Comment