From: Rick DeNatale Date: 2006-10-27T04:35:23+09:00 Subject: Re: debugging memory use and GC On 10/26/06, ara.t.howard@noaa.gov wrote: > it would be expensive, but i wonder of dumping the objects in objectspace > might be useful - since Marshal.dump already follows all references it seems > like a custom _dump method on object which could all themselves to a tree > might do the trick. in otherwords, if you dumped an object with a global tree > in contect then all objects being dumped as a result would add themselves to > this tree. after the dump, you simply keep a copy of the tree... Not sure, what I was suggesting was that the real goal is to somehow root out the reference path or path which is keeping an object from being reclaimed without making additional references. Another issue is that ObjectSpace.each_object can give you objects which aren't really alive: ick@frodo:/public/rubyscripts$ cat gctest.rb class Foo def initialize @iv = "bar" end end def make_foo p Foo.new end GC.enable make_foo ObjectSpace.each_object {|f| p f if Foo === f } ObjectSpace.garbage_collect puts "after gc" ObjectSpace.each_object {|f| p f if Foo === f } puts "done" rick@frodo:/public/rubyscripts$ ruby gctest.rb # # after gc # done I've played around with various versions of this, like each_object(Foo) and that instance of Foo with no apparent references to it seems to be sticking around for some reason. I instantiated Foo in the make_foo method to make sure that it wasn't still in the current stack frame. This really goes to show that the guarantee that the GC makes is not to free live objects, and not to free dead ones ASAP. It also shows why you shouldn't rely on finalization as part of application/system logic, since you never know when, or even if it will be called. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/