Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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]) * 1024
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%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 ))
@theinventor

This comment has been minimized.

Copy link
Owner Author

@theinventor theinventor commented Jul 24, 2018

example from my system right now:

[troy@MacBook-Pro Documents]$ python mem.py 
Wired Memory:		2775 MB
Active Memory:		5958 MB
Inactive Memory:	5078 MB
Free Memory:		410 MB
Real Mem Total (ps):	11756.645 MB

https://apple.stackexchange.com/questions/67031/isnt-inactive-memory-a-waste-of-resources

Per this, it seems Free+Inactive for me is 5,488MB out of 16GB - so it's about 1/3rd like the article says is a healthy amount.

In Windows they report this very differently - or maybe use memory differently - my current system in windows shows as:

Total Physical Memory:     32,438 MB
Available Physical Memory: 27,786 MB

So just using these 2 things, windows is using 15% of it's memory, but it's probably safe to assume mac is always trying to use 66%+ for efficiency.

So for v1 lets go with this;

totalmem = 16,000
avail = 5488
---- 5488/16000=0.343 (34% free, 66% used)
usedpercent = 66

So we're going to make a formula to soften this a bit and be a little closer to "windows reality" so that we can use the same counters for both platforms.

109.8591 - (input * 2.453182) + (0.02409091 * input * input)

where input is the "usedpercent"

[79] pry(main)> input = 66
=> 66
[80] pry(main)> 109.8591 - (input * 2.453182) + (0.02409091 * input * input)
=> 52.88909196
[81] pry(main)> input = 90
=> 90
[82] pry(main)> 109.8591 - (input * 2.453182) + (0.02409091 * input * input)
=> 84.20909099999999
[83] pry(main)> input = 99
^[[A=> 99
[84] pry(main)> 109.8591 - (input * 2.453182) + (0.02409091 * input * input)
=> 103.10909090999999

We can add a min 1 and max 100 if that helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.