Method One: Add Unit psapiprocedure Tform1.tmr1timer (sender:tobject); Begin EDT1. text:= format (' Memory use:%d KB ', [Getprocessmemuse (GetCurrentProcessId)]);//Method One End;function Tform1.getprocessmemuse (pid:cardinal): Cardinal; Method one varpmc:pprocess_memory_counters; Uses psapiprochandle:hwnd;isize:dword;begin Result: = 0; Isize: = SizeOf (_process_memory_counters); Getmem (PMC, isize); Try PMC^.CB: = isize; Prochandle: = OpenProcess (process_query_information or Process_vm_read, False, PID); The handle of the process object is obtained by the PID if Getprocessmemoryinfo (Prochandle, PMC, isize) then Result: = pmc^. WorkingSetSize; Result: =result div 1024; Finally Freemem (PMC); end;end;//********************************************* method Two: To add Unit Psapi, Tlhelp32;procedure TForm1.tmr1Timer (Sender : TObject); var str,str2:string;begin if (getprocessmemorysize (' Proj1.exe ', str,str2) then//method two parameters to write program name, bad begin ED T2. Text: = ' Current Memory usage: ' + str + ' KB '; End End;function tform1.getprocessmemorysize (processname:string; var Size,//Method two pn:string): Boolean;varpprocess:thandle; Memsize,t:integer;hprocsnap:thandle; ppmcsize:cardinal;pe32:tprocessentry32; Ppmc:pprocess_memory_counters;begin ppmcsize: = SizeOf (process_memory_counters); Getmem (PPMC, ppmcsize); Ppmc^.cb:= ppmcsize; Hprocsnap:=createtoolhelp32snapshot (th32cs_snapall, 0); If Hprocsnap=invalid_handle_value then Exit; Pe32.dwsize:=sizeof (PROCESSENTRY32); If Process32First (hprocsnap,pe32) =true then and Process32Next (HPROCSNAP,PE32) =true do BEGIN if UPP Ercase (Pe32.szexefile) =uppercase (ProcessName) THEN BEGIN Pprocess:=openprocess (process_all_access,fals E, Pe32.th32processid); if (Getprocessmemoryinfo (pprocess,ppmc,ppmcsize)) THEN BEGIN memsize:=ppmc^. WorkingSetSize Div 1024; t:= Length (IntToStr (memsize))-2; Size:=inttostr (memsize); Insert (', ', size,t); Pn:=processname; Freemem (PPMC); Result:=true; End ELSE begin FREEMEM (PPMC); Result:=false; End End End CloseHandle (pprocess); CloseHandle (hprocsnap); end;
8. Show how much memory the program uses.