[typo] memory profiling

Scott Laird scott at sigkill.org
Wed Aug 16 11:28:23 EDT 2006


I'm doing a bit of memory testing in my 4.1 tree, and I haven't found
anything all *that* exciting yet.  I'm using a objectspace profiler by
Ryan Davis, lightly modified to produce a bit more info.  Two things
seem obvious:

   1.  Cached hits don't leak anything.  The total number of objects
per type in the system doesn't change at all .

  2.  Uncached hits leak 1-3 strings per page.

I haven't tested admin yet.

Here's the profiler.  Start it from environment.rb and it'll log to a file.


class MemoryProfiler
  def self.start(delay=10)
    Thread.new do
      prev = Hash.new(0)
      curr = Hash.new(0)
      delta = Hash.new(0)

      file = File.open('log/memory_profiler.log','w')

      loop do
        begin
          GC.start
          curr.clear
          ObjectSpace.each_object do |o|
            curr[o.class] += 1 #Marshal.dump(o).size rescue 1
          end

          delta.clear
          (curr.keys + delta.keys).uniq.each do |k,v|
            delta[k] = curr[k]-prev[k]
          end

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

          delta.clear
          prev.clear
          prev.update curr
        rescue Exception => err
          STDERR.puts "** memory_profiler error: #{err}"
        end
        sleep delay
      end
    end
  end
end


More information about the Typo-list mailing list