PDA

View Full Version : Resource consumption awareness



masonwheeler
21-03-2008, 04:26 PM
If I hit <CTRL>-<ALT>-<DEL>, I can bring up the Task Manager, which tells me how much memory each program is using and how much CPU% it's consuming. How can my program get access to this data? I want to be able to optionally pre-load certain data before it's needed, but put a cap on it so it won't use up too much RAM. For this, I need to know the total amount of RAM the program's using.

arthurprs
21-03-2008, 05:36 PM
there are some winapi functions to retreive this kind of info, but i don't remember now =X

masonwheeler
21-03-2008, 07:04 PM
Yeah, I figured there would be. Does anyone know what it is?

Huehnerschaender
21-03-2008, 07:11 PM
Found this with a little google (didn't test it though):




uses psAPI;

procedure TForm1.Button1Click&#40;Sender&#58; TObject&#41;;
var
pmc&#58; PPROCESS_MEMORY_COUNTERS;
cb&#58; Integer;
begin
cb &#58;= SizeOf&#40;_PROCESS_MEMORY_COUNTERS&#41;;
GetMem&#40;pmc, cb&#41;;
pmc^.cb &#58;= cb;
if GetProcessMemoryInfo&#40;GetCurrentProcess&#40;&#41;, pmc, cb&#41; then
Label1.Caption &#58;= IntToStr&#40;pmc^.WorkingSetSize&#41; + ' Bytes'
else
Label1.Caption &#58;= 'Unable to retrieve memory usage structure';

FreeMem&#40;pmc&#41;;
end;

Memphis
21-03-2008, 07:12 PM
uses
psAPI;

function GetProcessMemorySize(sProcessName: string; var cMemSize: Cardinal): boolean;
var
hWndHandle, hProcID, hTmpHdn : HWND;
pPMC: PPROCESS_MEMORY_COUNTERS;
cSize: Cardinal;
begin
Result := False;

hWndHandle := FindWindow(nil, PChar(sProcessName));
if hWndHandle = 0 then exit;

cSize := SizeOf(PROCESS_MEMORY_COUNTERS);
GetMem(pPMC, cSize);
pPMC^.cb := cSize;
GetWindowThreadProcessId(hWndHandle, @hProcID);

hTmpHdn := OpenProcess(PROCESS_ALL_ACCESS, False, hProcID);
if GetProcessMemoryInfo(hTmpHdn, pPMC, cSize) then
cMemSize := pPMC^.WorkingSetSize
else
cMemSize := 0;

FreeMem(pPMC);
Result := True;
end;


usage:


var
cSize: Cardinal;
begin
if GetProcessMemorySize('softwarename', cSize) then


-Meka][Meka

masonwheeler
21-03-2008, 07:26 PM
Huehnerschaender: Thanks! That's just what I was looking for!

Memphis: Thanks too, but... that's scary. I think I'll use Huehnerschaender's version. :P