Skip to content

Instantly share code, notes, and snippets.

@vigo
Created January 27, 2012 18:00
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save vigo/1690045 to your computer and use it in GitHub Desktop.
Save vigo/1690045 to your computer and use it in GitHub Desktop.
"free" command for osx. Shows available memory
#!/usr/bin/env ruby
# encoding: utf-8
# By Uğur Özyılmazel, @vigobronx | @ugurozyilmazel
# http://vigodome.com | http://ugur.ozyilmazel.com | http://github.com/vigo
def get_paged_memory_usage(match_string, paging=4096)
mvar = 3
if match_string.split(/[^\w]/).length > 1
mvar = 4
end
paged_val = `vm_stat | grep "Pages #{match_string}:" | awk '{ print $#{mvar}}'`.to_i
gigabyte_val = (paged_val * paging) / 1024 / 1024 / 1000.0
end
installed_memory = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 1000.0
total_consumed = 0
['wired down', 'active', 'inactive'].each { |key| total_consumed += get_paged_memory_usage(key) }
printf("%.2fG", (installed_memory-total_consumed)) # returns in gigabytes
@ain
Copy link

ain commented Dec 22, 2012

To make this neat script available as the command free in your Terminal, see the guide at http://tekkie.flashbit.net/mac-os/free-m-alternative-for-mac-os-x

@vigo
Copy link
Author

vigo commented Jan 19, 2013

thank you @ain

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