Skip to content

Instantly share code, notes, and snippets.

@pvdb
Last active December 14, 2022 10:50
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pvdb/6240788 to your computer and use it in GitHub Desktop.
Save pvdb/6240788 to your computer and use it in GitHub Desktop.
Get real memory (resident set) used by current Ruby process
#
# This first version should work on Mac OS X and Linux, but it spawns a process
#
# http://stackoverflow.com/questions/7220896/
# https://github.com/rdp/os/blob/master/lib/os.rb#L127
# http://www.ruby-doc.org/core-2.0/Process.html
#
# A better - but more complicated - way to achieve the same is documented here:
#
# https://build.betterup.com/tracking-a-processs-memory-usage-in-ruby/
#
# the real memory (resident set) size of the process (in 1_024 byte units).
def Process.rss() `ps -o rss= -p #{Process.pid}`.chomp.to_i ; end
#
# This second version depends on the /proc filesystem, so won't work on Mac OS X
#
# https://www.kernel.org/doc/Documentation/filesystems/proc.txt
# >> Table 1-3 << Contents of the statm files (as of 2.6.8-rc3)
#
# Note that this implementation assumes that the size of memory
# pages is 4_096 bytes, ie. 4kB, which is a widespread default!
#
# However, it is probably best to check this assumption on your
# architecture, using `getconf PAGESIZE` or `getconf PAGE_SIZE`
#
# returns detailed information about the process memory usage (as recorded in the "/proc/$$/statm" proc fs file)
def Process.statm() Hash[%i{size resident shared trs lrs drs dt}.zip(open("/proc/#{Process.pid}/statm").read.split)] ; end
# the real memory (resident set) size of the process (in 1_024 byte units, assuming a 4kB memory page size)
def Process.rss() Process.statm[:resident].to_i * 4 ; end
#
# Some pretty formatting, on top of either of the above implementations
#
def Process.pretty_rss(rss = Process.rss) rss.to_s.gsub(/(\d)(?=(\d{3})+(\..*)?$)/,'\1,') + " kilobytes" ; end
@pvdb
Copy link
Author

pvdb commented Aug 15, 2013

Quick-n-dirty way to incorporate this info in an irb prompt:

irb(main):006:0> $old_to_s = self.to_s ; def self.to_s ; "#{$old_to_s}/#{Process.pretty_rss}" ; end
=> nil
irb(main/26,816 kilobytes):007:0> _

@renenw
Copy link

renenw commented Aug 17, 2021

very helpful, thanks.

Might be worth renaming the Process.rss to Process.rss_kb to make that clearer.

That is, if I'm understanding things properly!

@pvdb
Copy link
Author

pvdb commented Jan 10, 2022

Thanks for the feedback, @renenw, glad to hear you found it helpful.

Might be worth renaming the Process.rss to Process.rss_kb to make that clearer.

Yes, you're understanding things properly, and kilobytes is indeed the correct unit (see also the Process.pretty_rss() method included in the gist)

I may spend some time refactoring the gist to include the unit in the method name, as per your suggestion, or even provide different methods for returning the memory usage in different units

something akin to: https://github.com/schneems/get_process_mem/blob/main/lib/get_process_mem.rb#L65-L75

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