13

I have a Ruby app which (on Linux) uses the /proc filesystem to get information on its memory usage. Does anyone know how to get the same information for Mac OSX? The task_info system call looks promising but is there an equivalent available from Ruby?

To be clear, I'm looking for a system call, I don't want to kick off a process for this (sorry Lars!).

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
Chris McCauley
  • 25,824
  • 8
  • 48
  • 65

3 Answers3

24

Taken from http://laurelfan.com/2008/1/15/ruby-memory-usage:

memory_usage = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes 

Verified to work in both Linux and OS X.

This returns the number of bytes that the process has resident in memory, excluding any that are swapped out

To get the total virtual memory size including swap, change rss to vsz (tested in Linux, but not tested in OSX):

memory_usage = `ps -o vsz= -p #{Process.pid}`.to_i # in kilobytes 
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
Lars Haugseth
  • 14,721
  • 2
  • 45
  • 49
3

Referring to this answer it seems like you need to call proc_pidinfo(). I don't think there's a Ruby equivalent, so either you'll have to write a C-extension or use the ruby-ffi gem.

Other sources indicate Ruby 1.9.2 ships with a built in FFI -- but that version is not delivered with OS X.

Community
  • 1
  • 1
nimrodm
  • 23,081
  • 7
  • 58
  • 59
1

The OS gem has an rss_bytes method that works for Linux/windows/OS X ...

rogerdpack
  • 62,887
  • 36
  • 269
  • 388