Getting RAM usage of system processes (Windows)

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Getting RAM usage of system processes (Windows)

Post by Quin »

In my app, I need to get the RAM usage of some processes, including system ones. I currently have this code. It works on some processes, but a pretty frustratingly small subset, even when running as administrator. What's the proper way to do this? I found a couple code samples here, but they were all old and used functions my PB couldn't find :(

Code: Select all

Procedure GetRAMUsage(PID)
  Protected PMC.PROCESS_MEMORY_COUNTERS
  Protected Process = OpenProcess_(#PROCESS_ALL_ACCESS, #False, PID)
  If Not Process
    ProcedureReturn -1
  EndIf
  Protected Result = -1
    If GetProcessMemoryInfo(Process, @PMC, SizeOf(PROCESS_MEMORY_COUNTERS))
    Result = PMC\WorkingSetSize
  EndIf
  CloseHandle_(Process)
  ProcedureReturn Result
EndProcedure
Thanks!