From: Asher Date: 2010-08-28T03:07:39+09:00 Subject: [ruby-core:31903] Re: Garbage Collection Question It takes place in the Hash::Weak code in []: class Hash::Weak < Hash def []( key ) # get the stored ID - a FixNum, not an object reference to our weak-referenced object obj_id = super( key ) # theoretically this should cause non-referenced objects to get cleaned up # so long as nothing looks like a pointer or reference to it ObjectSpace.garbage_collect # now get our object from ID # if it had no references it should have been GC'd and we should get an # rb_eRangeError "is not id value" (expected) or "is recycled object" (possible) obj = ObjectSpace._id2ref( obj_id ) return obj end def []=( key, object ) # FixNum have a constant ID for value, so can't be copied and can't be garbage collected # so object.__id__ cannot be a reference to a child of object and therefore cannot prevent # garbage collection on the object super( key, object.__id__ ) end end Asher On Aug 27, 2010, at 2:05 PM, Evan Phoenix wrote: > Your examples do not include calling GC.start to force a GC, thus I wonder if this is the source of your problem. Remember that by default, the GC runs whenever it wants. So you can't depend on it to run an certain times. > > - Evan