Skip to content

Instantly share code, notes, and snippets.

@bobpoekert
Created January 13, 2013 00:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobpoekert/4521338 to your computer and use it in GitHub Desktop.
Save bobpoekert/4521338 to your computer and use it in GitHub Desktop.
Get info about available memory from Python on Linux.
def memory_info():
res = {}
for row in open('/proc/meminfo', 'r'):
k, v = row.split(':')
k = k.strip()
v = v.split()
if len(v) == 1:
v = int(v[0])
elif v[1] == 'kB':
v = int(v[0]) * 1024
elif v[1] == 'mB':
v = int(v[0]) * 1024 * 1024
res[k] = v
return res
@bobpoekert
Copy link
Author

>> memory_info()

{'Active': 497659904,
'Active(anon)': 142782464,
'Active(file)': 354877440,
'AnonHugePages': 0,
'AnonPages': 427524096,
'Bounce': 0,
'Buffers': 180330496,
'Cached': 629043200,
'CommitLimit': 6396567552L,
'Committed_AS': 2035286016,
'DirectMap2M': 926941184,
'DirectMap4k': 8380416,
'Dirty': 45056,
'HardwareCorrupted': 0,
'HighFree': 2417508352L,
'HighTotal': 3354845184L,
'HugePages_Free': 0,
'HugePages_Rsvd': 0,
'HugePages_Surp': 0,
'HugePages_Total': 0,
'Hugepagesize': 2097152,
'Inactive': 745345024,
'Inactive(anon)': 319483904,
'Inactive(file)': 425861120,
'KernelStack': 2187264,
'LowFree': 450445312,
'LowTotal': 860950528,
'Mapped': 49483776,
'MemFree': 2867953664L,
'MemTotal': 4215795712L,
'Mlocked': 0,
'NFS_Unstable': 0,
'PageTables': 5345280,
'SReclaimable': 56696832,
'SUnreclaim': 14139392,
'Shmem': 28622848,
'Slab': 70836224,
'SwapCached': 7176192,
'SwapFree': 4247822336L,
'SwapTotal': 4288671744L,
'Unevictable': 0,
'VmallocChunk': 112480256,
'VmallocTotal': 125829120,
'VmallocUsed': 12304384,
'Writeback': 0,
'WritebackTmp': 0}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment