Created
June 5, 2015 09:19
-
-
Save simleo/baffd30171e2a80d3639 to your computer and use it in GitHub Desktop.
A very non-portable way of tracking memory consumption in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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