Search code examples
rubymemoryprofilingpadrino

Profiling ruby memory usage on require call


I need to profile ruby gems memory usage. https://stackoverflow.com/a/164206/391229 suggests using system call to measure memory footprint, so ended up with aliasing require method and gathering stats.

The top of the startup script (in my case it's /usr/bin/padrino):

$memstat = {}
$memusage = `pmap #{Process.pid} | tail -1`[10,40].strip.to_i
$memstat['base'] = $memusage

alias :old_require :require
def require *args
  result = old_require *args
  oldmem = $memusage
  $memusage = `pmap #{Process.pid} | tail -1`[10,40].strip.to_i
  delta = $memusage - oldmem
  $memstat[args[0]] ||= 0
  $memstat[args[0]] += delta
  result
end

The event after loading everything (Padrino.after_load):

stat = $memstat.select{ |k,v| v>0 }.to_a.sort{ |a,b| a[1]<=>b[1] }
summ = 0
stat.each do |row|
  summ += row[1]
  puts "#{row[1].to_s.rjust(7)} KB: #{row[0]}"
end
puts summ.to_s.rjust(7) + ' KB'

The output I'm getting on invoking padrino console is:

        ...
   2120 KB: redcarpet.so
   2184 KB: socket.so
   2220 KB: etc
   2332 KB: addressable/idna/pure
   2740 KB: strscan
   2992 KB: haml/buffer
   3508 KB: pathname
   4240 KB: psych.so
   4252 KB: digest.so
   6028 KB: /home/ujif/swift/admin/app.rb
   6292 KB: zlib
   6704 KB: readline
   9116 KB: openssl.so
  12408 KB: do_mysql/do_mysql
  28164 KB: base
 145648 KB

Questions:

Is there any way to dig into base footprint?

Is there any cleaner approach to measure gems memory footprint on MRI ~> 1.9.2?

Any hints on improving my code?


Solution

  • Yep, it is the ruby stack. Try

    $ irb
    >> `pmap #{Process.pid} | tail -1`
    

    You got a similar result.

    Mine is: 144512K

    Instead if you run:

    $ ruby -e 'system "pmap #{Process.pid} | tail -1"'
    

    You a fewer value: 27788K (mine)

    So to inspect better what's happen go back to irb

    $ irb
    >> puts `pmap #{Process.pid}`
    

    When you need to track padrino deps load your lib inside irb

    $ cd my_padrino_project
    $ irb -r /path/to/my/lib.rb
    >> require_relative 'config/boot'
    

    and check your results.