Created
July 2, 2019 17:49
-
-
Save gchavez2/2da75f10b9d449922fa4d3cd971350f6 to your computer and use it in GitHub Desktop.
Report available RAM memory with Python
This file contains hidden or 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
#!/usr/bin/python | |
import subprocess | |
import re | |
# Get process info | |
ps = subprocess.Popen(['ps', '-caxm', '-orss,comm'], | |
stdout=subprocess.PIPE).communicate()[0].decode() | |
vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[ | |
0].decode() | |
# Iterate processes | |
processLines = ps.split('\n') | |
sep = re.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]) * 1000 | |
except: | |
rss = 0 # ignore... | |
rssTotal += rss | |
# Process vm_stat | |
vmLines = vm.split('\n') | |
sep = re.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%.3f GB' % (vmStats["Pages wired down"] / 1e9)) | |
print('Active Memory:\t\t%.3f GB' % (vmStats["Pages active"] / 1e9)) | |
print('Inactive Memory:\t%.3f GB' % (vmStats["Pages inactive"] / 1e9)) | |
print('Free Memory:\t\t%.3f GB' % (vmStats["Pages free"] / 1e9)) | |
print('Real Mem Total (ps):\t%.3f MB' % (rssTotal / 1e9)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment