Last active
December 14, 2022 10:50
-
-
Save pvdb/6240788 to your computer and use it in GitHub Desktop.
Get real memory (resident set) used by current Ruby process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
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!
Thanks for the feedback, @renenw, glad to hear you found it helpful.
Might be worth renaming the
Process.rss
toProcess.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
Quick-n-dirty way to incorporate this info in an
irb
prompt: