[Ruby] Memory profiling with Rails

Ryan Davis ryand-ruby at zenspider.com
Tue Aug 15 14:15:03 PDT 2006


On Aug 15, 2006, at 11:50 AM, Scott Laird wrote:

> One of the big things looming on the Typo Development Horizon is
> reducing our memory footprint.  Even after closing all of the known
> leaks, Typo is still growing too big--I've seen 60 MB dispatchers
> after a couple days' use.
>
> Does anyone have any suggestions on how to best profile memory use
> within Rails?  I'm planning on using something like this:
>
>   http://raggle.org/pipermail/raggle-dev/2003-August/000086.html
>
> But I'd love to discover that someone has a better tool for Ruby
> memory tracking.  Even if it requires patching Ruby itself or
> something similarly difficult.

When I've done memory profiling it consisted of 1 thread, 2 hashes  
(curr, prev), classes as keys, and fixnums as values. No extra memory  
required if I can help it. Keep it as simple as possible. The code at  
the link above is WAY to complex for such a simple task. Try  
something like:

def memory_profiler
   Thread.new do
     prev = Hash.new(0)
     curr = Hash.new(0)
     loop do
       curr.clear
       ObjectSpace.each_object do |o|
         curr[o.class] += 1
       end

       curr.each do |k,v|
         curr[k] -= prev[k]
       end

       puts "Top 20"
       curr.sort_by { |k,v| -v }[0..19].each do |k,v|
         printf "%+5d: %s\n", v, k.name unless v == 0
       end

       prev.clear
       prev.update curr
       sleep 2
     end
   end
end

It does use a sort_by, so that produces a bit extra junk... but that  
shouldn't be more than an extra array and hash per iteration.



More information about the Ruby mailing list