Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why there is a difference between used RAM in Task Manager and measure with psutil from Python?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • Python 3.12.2 x64
  • psutil 5.9.8 : library for process monitor and memory measurement
  • Running on Window 10 Pro x64

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading