Skip to content

Instantly share code, notes, and snippets.

@drew1kun
Last active April 10, 2017 17:17
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 drew1kun/c4a4add874723072fd19 to your computer and use it in GitHub Desktop.
Save drew1kun/c4a4add874723072fd19 to your computer and use it in GitHub Desktop.
script for getting system memory info over command line
#!/usr/bin/env python
from subprocess import Popen
from subprocess import PIPE
from re import compile
# Get process info
ps = Popen(['ps', '-caxm', '-orss,comm'], stdout=PIPE).communicate()[0]
vm = Popen(['vm_stat'], stdout=PIPE).communicate()[0]
# Iterate processes
processLines = ps.split('\n')
sep = compile('[\s]+')
rssTotal = 0 # kB
for row in range(1, len(processLines)):
rowText = processLines[row].strip()
rowElements = sep.split(rowText)
try:
rss = float(rowElements[0]) * 1024
except:
rss = 0 # ignore...
rssTotal += rss
# Process vm_stat
vmLines = vm.split('\n')
sep = compile(':[\s]+')
vmStats = {}
for row in range(1, len(vmLines)-2):
rowText = vmLines[row].strip()
rowElements = sep.split(rowText)
vmStats[(rowElements[0])] = int(rowElements[1].strip('\.')) * 4096
print 'Wired Memory:\t\t%d MB' % (vmStats["Pages wired down"]/1024/1024)
print 'Active Memory:\t\t%d MB' % (vmStats["Pages active"]/1024/1024)
print 'Inactive Memory:\t%d MB' % (vmStats["Pages inactive"]/1024/1024)
print 'Free Memory:\t\t%d MB' % (vmStats["Pages free"]/1024/1024)
print 'Real Mem Total (ps):\t%.3f MB' % (rssTotal/1024/1024)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment