From: "Victor \"Zverok\" Shepelev" Date: 2006-10-29T05:39:20+09:00 Subject: Re: How to understand why object is not collected? From: Joel VanderWerf [mailto:vjoel@path.berkeley.edu] Sent: Saturday, October 28, 2006 11:26 PM >Victor "Zverok" Shepelev wrote: >> From: Joel VanderWerf [mailto:vjoel@path.berkeley.edu] >> Sent: Saturday, October 28, 2006 10:52 PM >>> This is probably a situation where GC is being conservative. A simple >>> example: >>> >>> class Foo; end >>> >>> foo = Foo.new >>> foo = nil >>> >>> GC.start >>> >>> p ObjectSpace.each_object(Foo) {} # ==> 1 >> >> OK, I've got it. >> >>> In my experience, it's hard to write precise unit tests for GC behavior, >>> and you end up with some fuzziness. The important property of GC is >>> really the asymptotic behavior. >> >> Just for now, I need no precise unit test. I just want to know, would the >> objects of MyClass collected in principle. Can I achieve the goal >somehow? > >What about creating a lot of them in a loop, without keeping references? >Then I would expect GC to leave at most one instance. They can't be created by .new, they created in correspondence to some HTML DOM by C extension. Problem already solved by the trick: -- def test span = .... ObjectSpace.define_finalizer(span, proc{p "gc ok"}) span.detach span = nil end test GC.start p "end of code" -- Output corresponds to my expectations (gc ok => end of code) Thanks for your help! V.