Skip to content

Instantly share code, notes, and snippets.

@simleo
Created June 5, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simleo/baffd30171e2a80d3639 to your computer and use it in GitHub Desktop.
Save simleo/baffd30171e2a80d3639 to your computer and use it in GitHub Desktop.
A very non-portable way of tracking memory consumption in Python
import os
PAGESIZE = os.sysconf("SC_PAGE_SIZE") # BYTES
STATM_FIELDS = ["size", "resident", "share", "text", "lib", "data", "dt"]
def meminfo(pid=None):
if pid is None:
pid = os.getpid()
pid = str(pid)
with open("/proc/%s/statm" % pid) as f:
info = f.next()
info = [int(i)*PAGESIZE for i in info.split()]
return dict(zip(STATM_FIELDS, info))
def get_total_mem(multiple=''):
v = 1
mmap = {}
for k in '', 'K', 'M', 'G':
mmap[k] = v
v <<= 10
mmap['T'] = v
return meminfo()['size'] / mmap[multiple]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment