From: Garthy D Date: 2013-01-27T16:21:31+09:00 Subject: Re: Soft object reference for mark and sweep Hi Robert, On 27/01/13 02:46, Robert Klemme wrote: > On Sat, Jan 26, 2013 at 9:52 AM, Nokan Emiro wrote: >> >>> >>> Any ideas as to a good way or suitable mechanisms to implement this? >> >> >> Just a silly one: if you seldom use that Hash in the store, you can >> eliminate it. You could >> still lookup your objects by iterating trough ObjectSpace and find them. >> But this way the >> GC will be able to drop objects if there's no reference to them. Basically >> what you do here >> the same old stuff: a trade off between memory and CPU. You have to decide >> if lookup >> speed is more important than memory footprint or the other way around. >> (...or wait for a >> less silly answer here... :) > > I think using WeakReference is even better because ObjectSpace lookup > is slow and does not work in some circumstances (JRuby with specific > settings). > > http://www.ruby-doc.org/stdlib-1.9.3/libdoc/weakref/rdoc/index.html > > If need by one can always create a special Hash wrapper which converts > from and to WeakReference on insert and retrieval. Thanks for that. I didn't even know WeakRef existed! That's going to open up a wide range of solutions now. Looking at the implementation, it's using finalizers, and I had been wondering if finalizers were going to lead to a potential solution. The WeakRef interface worries me though. You'd normally expect to see just one call on such a thing (lock, which turns weak to strong/fail) and maybe a check call, with a big warning that the result might change post-check. A delegation-style interface with a check only seems a bit unusual. However, I may just not have a proper understanding of how it works yet. Still- it's opened up a bunch of possibilities and ideas- lots of things to look into and explore. Thankyou again Robert for yet another one of your excellent suggestions. :) Cheers, Garth