0

I am having trouble retrieving memory usage of my system using psutil.

When I run my code it retrieves the cpu usage correctly, but when it tries to get the memory it throws an AccessDenied error.

code:

if(pi['name']=='java.exe'):
    pro=psutil.Process(0)
    cpu=pro.cpu_percent(1)
    memory=pro.memory_full_info().uss/(1024*1024)
    return memory,cpu

Error message:

psutil.AccessDenied (pid=0)

How can i get memory usage?

Oliver.R
  • 1,282
  • 7
  • 17
  • This suggests that your script doesn't have access to get that processes information. Have you tried running your script as an admin / root? – Oliver.R Mar 19 '20 at 04:37
  • No. i am running on VS code. – JAYAPRAKASH Mar 19 '20 at 04:44
  • How can i run as a admin? – JAYAPRAKASH Mar 19 '20 at 04:47
  • For Windows, [this SO thread](https://stackoverflow.com/a/39948482/10993299) provides information on running a VSCode instance as admin every time it launches. For a one-time admin run of VSCode, shift+right-click VSCode, and "Run as Administrator". – Oliver.R Mar 19 '20 at 04:50
  • I'm also assuming that none of the basic [`memory_info()`](https://psutil.readthedocs.io/en/latest/#psutil.Process.memory_info) details will provide sufficient information for you (they don't require elevated priveleges). – Oliver.R Mar 19 '20 at 04:55
  • whit administration also the same exception .. – JAYAPRAKASH Mar 19 '20 at 05:10
  • pid 0 aka *System Idle Process* requires `SYSTEM` privileges -- higher than admin. Do you really want to know its memory usage, or do you actually want to know `java.exe`'s memory usage? See my answer. – GordonAitchJay Mar 19 '20 at 06:43

1 Answers1

0

psutil.Process() takes one argument - a process identifier (PID). If it is called without an argument or with None, then the pid of the current process is used.

On Window, pid 0 is the The System Idle Process, which requires SYSTEM privileges.

pro=psutil.Process(0) # here's your problem

If you want to get the memory/cpu usage of java.exe, you'll have to get the pid, then use psutil. In the example below, I have used firefox.exe:

import psutil
from ctypes import *
from ctypes.wintypes import *

class PROCESSENTRY32W(Structure):
    _fields_ = [("dwSize", DWORD),
                ("cntUsage", DWORD),
                ("th32ProcessID", DWORD),
                ("th32DefaultHeapID", POINTER(ULONG)),
                ("th32ModuleID", DWORD),
                ("cntThreads", DWORD),
                ("th32ParentProcessID", DWORD),
                ("pcPriClassBase", LONG),
                ("dwFlags", DWORD),
                ("szExeFile", c_wchar * MAX_PATH)]

def get_processes():
    TH32CS_SNAPPROCESS = 0x00000002
    INVALID_HANDLE_VALUE = -1

    CreateToolhelp32Snapshot = windll.kernel32.CreateToolhelp32Snapshot
    CreateToolhelp32Snapshot.argtypes = [DWORD, DWORD]
    CreateToolhelp32Snapshot.restype = HANDLE

    Process32FirstW = windll.kernel32.Process32FirstW
    Process32FirstW.argtypes = [HANDLE, POINTER(PROCESSENTRY32W)]
    Process32FirstW.restype = BOOL

    Process32NextW = windll.kernel32.Process32NextW
    Process32NextW.argtypes = [HANDLE, POINTER(PROCESSENTRY32W)]
    Process32NextW.restype = BOOL

    CloseHandle = windll.kernel32.CloseHandle
    CloseHandle.argtypes = [HANDLE]
    CloseHandle.restype = BOOL

    snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    if snapshot == INVALID_HANDLE_VALUE:
        return []

    try:
        process_entry = PROCESSENTRY32W()
        process_entry.dwSize = sizeof(PROCESSENTRY32W)
        if not Process32FirstW(snapshot, byref(process_entry)):
            return []

        processes = []
        while Process32NextW(snapshot, byref(process_entry)):
            processes.append((process_entry.th32ProcessID, process_entry.szExeFile))
    finally:
        CloseHandle(snapshot)
        #print("Closed handle")

    return processes

for pid, name in get_processes():
    if name == "firefox.exe":
        pro = psutil.Process(pid)
        cpu = pro.cpu_percent(1.0)
        memory = pro.memory_full_info().uss/(1024*1024)
        print(memory, cpu)

Output:

471.2890625 1.6
127.15234375 25.0
18.50390625 0.0
668.09375 0.0
706.6875 0.0
512.671875 0.0
648.953125 4.7
1037.0859375 4.7
1160.1015625 98.4
337.46484375 0.0
1418.72265625 1.6
0.90625 0.0
0.90625 0.0
GordonAitchJay
  • 4,640
  • 1
  • 14
  • 16