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(Sender: TObject);
var
pmc: PPROCESS_MEMORY_COUNTERS;
cb: Integer;
begin
cb := SizeOf(_PROCESS_MEMORY_COUNTERS);
GetMem(pmc, cb);
pmc^.cb := cb;
if GetProcessMemoryInfo(GetCurrentProcess(), pmc, cb) then
Label1.Caption := IntToStr(pmc^.WorkingSetSize) + ' Bytes'
else
Label1.Caption := 'Unable to retrieve memory usage structure';
FreeMem(pmc);
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
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.