On 10/26/06, [email protected] [email protected] 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
#<Foo:0xb7dc1804 @iv=“bar”>
#<Foo:0xb7dc1804 @iv=“bar”>
after gc
#<Foo:0xb7dc1804 @iv=“bar”>
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/