Conversión de Gant código C++, a la consola de aplicación en Delphi:
program MemoryProcessCMD;
{* Based in Gant(https://stackoverflow.com/users/12460/gant) code*}
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
psapi,
Windows;
procedure PrintMemoryInfo(processID: DWORD);
var
hProcess: THandle;
pmc: PROCESS_MEMORY_COUNTERS;
total: DWORD;
begin
// Print the process identifier.
Writeln(format('Process ID: %d', [processID]));
// Print information about the memory usage of the process.
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE,
processID);
if (hProcess = 0) then
begin
exit;
end;
if (GetProcessMemoryInfo(hProcess, @pmc, SizeOf(pmc))) then
begin
Writeln(format(#09'PageFaultCount: 0x%.8X', [pmc.PageFaultCount]));
Writeln(format(#09'PeakWorkingSetSize: 0x%.8X', [pmc.PeakWorkingSetSize]));
Writeln(format(#09'WorkingSetSize: 0x%.8X', [pmc.WorkingSetSize]));
Writeln(format(#09'QuotaPeakPagedPoolUsage: 0x%.8X',
[pmc.QuotaPeakPagedPoolUsage]));
Writeln(format(#09'QuotaPagedPoolUsage: 0x%.8X',
[pmc.QuotaPagedPoolUsage]));
Writeln(format(#09'QuotaPeakNonPagedPoolUsage: 0x%.8X',
[pmc.QuotaPeakNonPagedPoolUsage]));
Writeln(format(#09'QuotaNonPagedPoolUsage: 0x%.8X',
[pmc.QuotaNonPagedPoolUsage]));
Writeln(format(#09'PagefileUsage: 0x%.8X', [pmc.PagefileUsage]));
Writeln(format(#09'PeakPagefileUsage: 0x%.8X', [pmc.PeakPagefileUsage]));
Writeln(format(#09'PagefileUsage: 0x%.8X', [pmc.PagefileUsage]));
end;
CloseHandle(hProcess);
end;
var
aProcesses: array [0 .. 1024] of DWORD;
cbNeeded, cProcesses: DWORD;
i: Integer;
begin
try
// Get the list of process identifiers.
if (not EnumProcesses(@aProcesses, SizeOf(aProcesses), &cbNeeded)) then
halt(1);
// Calculate how many process identifiers were returned.
cProcesses := cbNeeded div SizeOf(DWORD);
// Print the memory usage for each process
for i := 0 to cProcesses - 1 do
begin
PrintMemoryInfo(aProcesses[i]);
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Este método es válido cuando se usa FastMM4 o delphi 2006+, pero si no está utilizando FastMM4, ¡debería considerar comenzar a usarlo! –
Esta puede ser la mejor y más útil respuesta que he visto en StackOverflow. Ojalá pudiera votarlo 100 veces. –
Además de lo anterior, este método muestra la cantidad de memoria * asignada * por la aplicación, no la cantidad de memoria utilizada (como memoria asignada por dlls de terceros, ocx/COM, etc.) Para eso, una solución mucho más confiable es el dado por Jim McKeeth a continuación, siempre que MemCounters.PagefileUsage también se agregue al resultado. – ciuly