Total memory used by Python process?

Total memory used by Python process?

Here is a useful solution that works for various operating systems, including Linux, Windows, etc.:

import os, psutil
process = psutil.Process(os.getpid())
print(process.memory_info().rss)  # in bytes 

Notes:

  • do pip install psutil if it is not installed yet

  • handy one-liner if you quickly want to know how many MB your process takes:

    import os, psutil; print(psutil.Process(os.getpid()).memory_info().rss / 1024 ** 2)
    
  • with Python 2.7 and psutil 5.6.3, it was process.memory_info()[0] instead (there was a change in the API later).

For Unix based systems (Linux, Mac OS X, Solaris), you can use the getrusage() function from the standard library module resource. The resulting object has the attribute ru_maxrss, which gives the peak memory usage for the calling process:

>>> resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
2656  # peak memory usage (kilobytes on Linux, bytes on OS X)

The Python docs dont make note of the units. Refer to your specific systems man getrusage.2 page to check the unit for the value. On Ubuntu 18.04, the unit is noted as kilobytes. On Mac OS X, its bytes.

The getrusage() function can also be given resource.RUSAGE_CHILDREN to get the usage for child processes, and (on some systems) resource.RUSAGE_BOTH for total (self and child) process usage.

If you care only about Linux, you can alternatively read the /proc/self/status or /proc/self/statm file as described in other answers for this question and this one too.

Total memory used by Python process?

On Windows, you can use WMI (home page, cheeseshop):

def memory():
    import os
    from wmi import WMI
    w = WMI(.)
    result = w.query(SELECT WorkingSet FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess=%d % os.getpid())
    return int(result[0].WorkingSet)

On Linux (from python cookbook http://code.activestate.com/recipes/286222/:

import os
_proc_status = /proc/%d/status % os.getpid()

_scale = {kB: 1024.0, mB: 1024.0*1024.0, KB: 1024.0, MB: 1024.0*1024.0}

def _VmB(VmKey):
    Private.
    global _proc_status, _scale
     # get pseudo file  /proc/<pid>/status
    try:
        t = open(_proc_status)
        v = t.read()
        t.close()
    except:
        return 0.0  # non-Linux?
     # get VmKey line e.g. VmRSS:  9999  kBn ...
    i = v.index(VmKey)
    v = v[i:].split(None, 3)  # whitespace
    if len(v) < 3:
        return 0.0  # invalid format?
     # convert Vm value to bytes
    return float(v[1]) * _scale[v[2]]

def memory(since=0.0):
    Return memory usage in bytes.
    return _VmB(VmSize:) - since

def resident(since=0.0):
    Return resident memory usage in bytes.
    return _VmB(VmRSS:) - since

def stacksize(since=0.0):
    Return stack size in bytes.
    return _VmB(VmStk:) - since

Leave a Reply

Your email address will not be published.