Ruby Memory Usage

January 15th, 2008

Tracking down a memory leak? Here’s a way to find the memory usage of the current process on Ruby:


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

-o rss= asks ps to print only the RSS (Resident Set Size, or physical memory used). You could also use vsz/vsize (virtual memory). The hanging = sign sets the header text to a blank string so you don’t have to filter out the header line.

-p #{Process.pid} limits the ps to only show the current process.

The backticks are kind of hacky, but this cuts down on the piping and grepping. It works on all of the unixes I’ve tried (Linux, FreeBSD, OSX), but of course ps is notoriously nonstandardized.

1 Response to “Ruby Memory Usage”

  1. Phil Says:
    Hey - thanks... Got to use this today for a stupid java thread memory issue. Not sure why I'm playing with java, but oh well...

Sorry, comments are closed for this article.