Joe Van Dyk wrote:

> Hi,
> 
> My application's size in memory seems to be increasing by about 250KB
> a second.  This is not a good thing.
> 
> Are there any tools or strategies that I can use to track down the leak?

That's a large leak.  My approach is to first step back and walk through the
code on paper, finding all of the areas where I _might_ be creating
references that don't get cleaned up.  Anything that I think _might_ be a
problem I then try to isolate and test individually.  I'll put diagnostic
logging in to check object counts or specific contents of hashes or LRU
caches or whatever else I might suspect to be a problem.

In bad cases, you can use ObjectSpace to dump every object known to the the
Ruby interpreter at some specific point in your code, and then skim through
that output to see if you can spot references that are accumulating which
should not be.

File.open('/tmp/objects.out','a+') do |fh|
  fh.puts '________'
  ObjectSpace.each_object do |obj|
    fh.puts "#{obj.class.name} ::: #{obj.inspect}"
  end
end

If you are dealing with a C extension, walk through the code.  Look for
places where memory is allocated but never freed.  When one is used to
programming in a language with garbage collection, it is very easy to
allocate memory in C without making sure that it'll be freed when no longer
needed.


Kirk Haines