Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Created March 22, 2012 23:22
Show Gist options
  • Save jaytaylor/2165430 to your computer and use it in GitHub Desktop.
Save jaytaylor/2165430 to your computer and use it in GitHub Desktop.
Add this to your .bashrc or .bash_profile to get a `free` command in OS-X. Originally found at http://apple.stackexchange.com/questions/4286/is-there-a-mac-os-x-terminal-version-of-the-free-command-in-linux-systems
##
# How about a `free` command if we're on OS-X?
#
if [ -z "`which free`" ]; then
freePython="
import re, subprocess
# Get process info
ps = subprocess.Popen(['ps', '-caxm', '-orss,comm'], stdout=subprocess.PIPE).communicate()[0]
vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0]
# Iterate over all 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
# Display output
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)"
alias free="python -c "'"'$freePython'"'
unset freePython
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment