I'm writing a script to log the amount of memory used by a certain application over time, and encountered a problem that measured memory with Python is always higher than indicated in Windows Task Manager by 4-10 MB.
I wonder why it is so, and how to measure it correctly?
Used tools:
As per documentation example I made a function:
def get_memory_use_of_process(target_process_name ):
try:
for proc in psutil.process_iter(['name', 'memory_percent', 'memory_info']):
if target_process_name.lower() in proc.info['name'].lower().strip():
# On Windows proc.info['memory_info'][0] matches “Mem Usage” column of taskmgr.exe
# On Windows proc.info['memory_info'][1] matches “Mem Usage” “VM Size” column of taskmgr.exe
return proc.info['memory_info'][0]/1024/1024 # to convert to MB from B
except:
sys.exit()
else:
sys.exit("No needed process found")
But result is always higher than one returned by Task Manager. What to measure to make it the same?
P.S. This is "debugging" for discussion with provider of software and I would like to have the same metric as Windows Task Manager for proper comparison by them and by us.
The difference in memory usage reported by the Windows Task Manager and psutil could be due to how each tool calculates and reports memory usage. The Windows Task Manager might not report certain types of memory that psutil does.
On the other hand, the Windows Task Manager might be reporting a different type of memory usage. For instance, it might be reporting the working set size, which is the amount of memory currently in use and in the physical RAM2. also you can see this psutil windows process memory usage
To get a more accurate comparison, you might want to consider using other memory metrics provided by psutil, such as memory_full_info() which provides more detailed memory usage information. You can then compare these values with the different memory metrics provided in the Task Manager.
And if you want to use memory_full_info(), here is a example:
def get_memory_use_of_process(target_process_name):
try:
for proc in psutil.process_iter(['name', 'memory_full_info']):
if target_process_name.lower() in proc.info['name'].lower().strip():
mem_info = proc.info['memory_full_info']
return mem_info.uss / 1024 / 1024 # USS in MB
except:
sys.exit()
else:
sys.exit("No needed process found")
mem_info.uss gives the Unique Set Size (USS), which is the memory which is unique to a process and which would be freed if the process was terminated right now4.
And as all, there is not necessarily a "correct" and "incorrect" measurement, it depends on what is measured.