From: Yohanes Santoso Date: 2005-07-08T05:47:03+09:00 Subject: Re: Collecting garbage "Eustaquio Rangel de Oliveira Jr." writes: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi! > > Thanks for the answer. :-) > > | Ruby's GC is lazy and conservative. Lazy meaning, it does not > | exhaustively try to do path-finding on each object because for some > | objects, path-finding is an expensive process. Conservative meaning, > | if it is not doing a path-finding on an object, that object is not > | freed. > | > | Hate it or love it. It has both advantages and disadvantages. You have > | just shown a disadvantage. But it can be mitigated by exiting the > | scope which should make path-finding to those objects cheaper. > > I'll search about lazy and conversative gc's. :-) > Just another question: if I use a method there, and not a begin/end block, > like this > > - -------------------------------------------------------------------------- > def test > c1 = CustomObject.new("1",CustomObject.new("2",CustomObject.new("3"))) > c4 = CustomObject.new("4",CustomObject.new("5")) > c6 = CustomObject.new("6") > c1.next.next.next = c1 # comment this and check again > list > end > > test > GC.start > sleep(1) > list > - -------------------------------------------------------------------------- > > After run this, only c6 remains. It's about scope also? > Funny, I couldn't reproduce what you are seeing. I cut and paste the code you have and run it: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv /tmp $ ruby1.8 ./gc.rb Listing all CustomObject's with ObjectSpace Object 6 (-604561642) points to nothing () Object 4 (-604561622) points to 5 (-604561612) Object 5 (-604561612) points to nothing () Object 1 (-604561582) points to 2 (-604561572) Object 2 (-604561572) points to 3 (-604561562) Object 3 (-604561562) points to 1 (-604561582) 6 objects found Listing all CustomObject's with ObjectSpace 0 objects found ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Nothing remains. Probably you are seeing c6 remaining because you were playing with your code like so: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv def test c1 = CustomObject.new("1",CustomObject.new("2",CustomObject.new("3"))) c4 = CustomObject.new("4",CustomObject.new("5")) c6 = CustomObject.new("6") end test GC.start sleep(1) list ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Notice that c6 is returned from test, so the object refered to by c6 is now accessible from where you invoke GC.start and that is the original problem. YS.