Get process memory usage


/ Published in: Delphi
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function GetProcessMemorySize(sProcessName: string; var nMemSize: Cardinal): Boolean;
  2. var
  3. l_nWndHandle, l_nProcID, l_nTmpHandle: HWND;
  4. l_pPMC: PPROCESS_MEMORY_COUNTERS;
  5. l_pPMCSize: Cardinal;
  6. begin
  7. l_nWndHandle := FindWindow(nil, PChar(sProcessName));
  8. if l_nWndHandle = 0 then
  9. begin
  10. Result := False;
  11. Exit;
  12. end;
  13. l_pPMCSize := SizeOf(PROCESS_MEMORY_COUNTERS);
  14. GetMem(l_pPMC, l_pPMCSize);
  15. l_pPMC^.cb := l_pPMCSize;
  16. GetWindowThreadProcessId(l_nWndHandle, @l_nProcID);
  17. l_nTmpHandle := OpenProcess(PROCESS_ALL_ACCESS, False, l_nProcID);
  18. if (GetProcessMemoryInfo(l_nTmpHandle, l_pPMC, l_pPMCSize)) then
  19. nMemSize := l_pPMC^.WorkingSetSize
  20. else
  21. nMemSize := 0;
  22. FreeMem(l_pPMC);
  23. CloseHandle(l_nTmpHandle);
  24. Result := True;
  25. end;
  26.  
  27. function GetCurrentProcessMemorySize(var nMemSize: Cardinal): Boolean;
  28. var
  29. nWndHandle, nProcID, nTmpHandle: HWND;
  30. pPMC: PPROCESS_MEMORY_COUNTERS;
  31. pPMCSize: Cardinal;
  32. begin
  33. nWndHandle := Application.Handle;
  34. if nWndHandle = 0 then
  35. begin
  36. Result := False;
  37. Exit;
  38. end;
  39. pPMCSize := SizeOf(PROCESS_MEMORY_COUNTERS);
  40. GetMem(pPMC, pPMCSize);
  41. pPMC^.cb := pPMCSize;
  42. GetWindowThreadProcessId(nWndHandle, @nProcID);
  43. nTmpHandle := OpenProcess(PROCESS_ALL_ACCESS, False, nProcID);
  44. if (GetProcessMemoryInfo(nTmpHandle, pPMC, pPMCSize)) then
  45. nMemSize := pPMC^.WorkingSetSize
  46. else
  47. nMemSize := 0;
  48. FreeMem(pPMC);
  49. CloseHandle(nTmpHandle);
  50. Result := True;
  51. end;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.