From: Roger Pack Date: 2010-08-28T01:13:50+09:00 Subject: [ruby-core:31897] Re: Garbage Collection Question (Fixed: Plaintext) > Let's stick with the example: a local variable is set as a reference to an object, the local variable is then set to nil so there is no longer a live reference to the object. No other ruby space commands have gone on, so unless Ruby is keeping junk behind the scenes, there should be no references - not on the Ruby stack, not on the C stack. How does this object get collected? As shown by the example, it is missed during the next attempt to GC (as well as any repeated attempts at this point). Unfortunately you'll have to assume that there is still some "bad ref" around to it. One trick is to try and nest whatever you "violently" need to be collected deep in some sub routine, then call GC.start *after* recursing back up from that sub routine. ex: >> class A; end >> def go; A.new; end >> go => # >> ObjectSpace.each_object(A).each{|o| p o } # => 1 >> GC.start >> ObjectSpace.each_object(A).each{|o| p o } => 0 you can see that it was safely collected. NB that if you define anything that is somehow attached to a constant, like >> class A; B = 33; end B will never be collected. also note that 1.8.x collects more poorly than 1.9.x and 1.8.x with MBARI (like REE), because of extra ghost references. GL! -r