tuxdna
tuxdna

Reputation: 8487

A good way to get memory usage of a Ruby process in Linux from within itself?

I wanted a good way to get memory usage of a Ruby process in Linux from within itself. For that I defined a Ruby function vmrss() which returns the Resident Set Size of current process:

#!/usr/bin/env ruby
def vmrss
    File.readlines('/proc/self/status').select {|l| l =~ /VmRSS/}.first.split[1..-1].join(" ")
end

puts "My current memory consumption: #{vmrss}"

Are there any better ways?

Upvotes: 1

Views: 506

Answers (1)

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

You could use the proc-wait3 library, which adds Process.getrusage. It would save you implementing the low-level stuff yourself, but if you only want memory usage, including a library might be overkill, especially given that hasn't been updated in a while.

Upvotes: 1

Related Questions