From: Kurt Stephens Date: 2011-01-26T09:52:42+09:00 Subject: [ruby-core:34857] [Ruby 1.9-Bug#4168] WeakRef is unsafe to use in Ruby 1.9 Issue #4168 has been updated by Kurt Stephens. > 01/25/2011 04:40 PM - Charles Nutter > Can you provide a short description of the complete "soft" heuristic > as of this commit? Even on JVM, it's unspecified, but each JVM does > spell out what constitutes a "soft" reference for them. * Each SoftReference#_mri_gc_left and SoftRefernce#_mri_gc_ttl are set to some arbitrary value: SoftReference._mri_gc_ttl (default = 10). * If SoftReference#object was traversed since last GC, SoftReference#_mri_gc_left is reset to SoftRereference#_mri_gc_ttl. * If SoftReference#object was not traversed since last GC, SoftReference#_mri_gc_left is decremented by 1. * If memory pressure occurred, SoftReference#_mri_gc_left is also decremented by 0.5 * average of all SoftReference#_mri_gc_left. * "Memory pressure" is whenever MRI allocates a new GC heap. * When SoftReference#_mri_gc_left reaches 0, SoftReference#object= nil. * When SoftReference#object == nil or #object is not reachable, ReferenceQueues are notified. The values of 10 and 0.5 above are off-the-cuff tuning parameters. I have no empirical data to warrant the values, but the test shows that the algorithm works. If the test also passes under JRuby, awesome! :) See the pseudo-code here: https://github.com/kstephens/ruby/blob/trunk-weakref-c/ext/reference/lib/reference/mri/soft_reference.rb I'm not sure I like the heuristics. I think others could come up with something better in pure Ruby, thus the pseudo-code may become the implementation. > When you think your tests are stabilizing, I will implement the same > API in JRuby and we'll see how it looks. The unit tests have MRI-specific assertions in them -- I'll abstract them out. Aside: Q: How should HardReference behave when associated with a ReferenceQueue? ---------------------------------------- http://redmine.ruby-lang.org/issues/show/4168 ---------------------------------------- http://redmine.ruby-lang.org