Development Tips

Some usefull tricks for OSX People.

Show Free Memory on OSX

| Comments

I was looking for Ubuntu ’s free command for osx lion. I googled it as usual. I’ve seen that, there was a command called alloc and it is not available at the moment. I decided to write my own script in .

This is almost my first Ruby attempt! ( I wrote in Python )

Based on “Activity Monitor.app” on OS X, used memory is a sum of:

  • Wired
  • Active
  • Inactive

memories. I managed to find out installed memory via sysctl -n hw.memsize . Here is the script that i wrote:

Gist is available here

free-memory.rb link
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/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

I embedded it to my prompt shell aka $PS1 . I’ve checked and compared the results via htop and top commands. I get the same exac values. Due to OSX Lion, memory goes wild!!!. Especially Firefox leeches it abnormally. This little script kinda warns me that it is time to restart Firefox or close it for a while!

Update

Ain Tohvri mentioned my little tool here .

Comments