From: Asher Date: 2010-08-28T03:04:11+09:00 Subject: [ruby-core:31901] Re: Garbage Collection Question Trying your example 1000 times simply loops infinitely on my ruby. Not sure why. Trying it 10.times and then 2.times works - throws expected error on first attempt to retrieve. Trying it 1.times does not - returns uncollected object. Your example of 1000 times causes it to work on the 33rd try. What gives? or is your point that there is no way to predict this? If that's the case, why does it work the first time consistently with my attempts 2.times, 10.times, etc.? Asher On Aug 27, 2010, at 1:43 PM, brabuhr@gmail.com wrote: > On Fri, Aug 27, 2010 at 12:33 PM, Asher wrote: >> On Aug 27, 2010, at 12:09 PM, Evan Phoenix wrote: >>> It doesn't happen with every object allocated in a script body, only sometimes. >> >> No, it happens _every_ time. See examples. > > Modified original program: > > ################################################## > > weak_hash = Hash::Weak.new > > class TestClass > end > > 1_000.times do |n| > test_object = TestClass.new > > weak_hash[ n ] = test_object > valid_key = weak_hash[ n ] > p valid_key > end > > class AnotherClass > end > > test_object = nil > > 1_000.times do |n| > print "getting test object (#{n}) (should fail with rb_eRangeError): " > invalid_key = weak_hash[ n ] > p invalid_key > # error - returns valid key > end > > ################################################## > > Output: > > $ ruby -v gc.rb > ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux] > # > # > # > [...] > # > # > # > getting test object (0) (should fail with rb_eRangeError): > # > getting test object (1) (should fail with rb_eRangeError): > # > getting test object (2) (should fail with rb_eRangeError): > # > [...] > getting test object (31) (should fail with rb_eRangeError): > # > getting test object (32) (should fail with rb_eRangeError): > # > gc.rb:15:in `_id2ref': 0xdb9fbf04 is recycled object (RangeError) > from gc.rb:15:in `[]' > from gc.rb:54 > from gc.rb:52:in `times' > from gc.rb:52 > getting test object (33) (should fail with rb_eRangeError): >