Skip to content

Instantly share code, notes, and snippets.

@jacqueswww
Created February 3, 2017 13:26
Show Gist options
  • Save jacqueswww/3b2a824cd895580202b999053b26dfb2 to your computer and use it in GitHub Desktop.
Save jacqueswww/3b2a824cd895580202b999053b26dfb2 to your computer and use it in GitHub Desktop.
Quickly see memory usage from within a python process (Linux only)
import os
class MemoryUsage:
# thanks to http://code.activestate.com/recipes/286222/
def __init__(self):
self._proc_status = '/proc/%d/status' % os.getpid()
self._scale = {
'kB': 1024.0, 'mB': 1024.0 * 1024.0,
'KB': 1024.0, 'MB': 1024.0 * 1024.0
}
def _VmB(self, VmKey):
'''Private.
'''
# get pseudo file /proc/<pid>/status
try:
t = open(self._proc_status)
v = t.read()
t.close()
except:
return 0.0 # non-Linux?
# get VmKey line e.g. 'VmRSS: 9999 kB\n ...'
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]) * self._scale[v[2]]
def memory(self, since=0.0):
'''Return memory usage in bytes.
'''
return self._VmB('VmSize:') - since
def resident(self, since=0.0):
'''Return resident memory usage in bytes.
'''
return self._VmB('VmRSS:') - since
def stacksize(self, since=0.0):
'''Return stack size in bytes.
'''
return self._VmB('VmStk:') - since
def print(self):
print("VM: {} RSS: {} Stack: {}".format(
self.memory(), self.resident(), self.stacksize()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment