From: Robert Klemme Date: 2010-04-14T01:50:06+09:00 Subject: Re: Memoize and GC On 04/13/2010 06:26 PM, Intransition wrote: > I have a quick question. I just re-read: > > http://www.engineyard.com/blog/2010/memoization-and-id2ref/ > > I am wondering about garbage collection. Am I right to think that > using > > def foo > @foo ||= something > end > > will mean that @foo be GC'd when the object is GC'd, but if I do > > FOO = [] > > def foo > FOO[object_id] ||= something > end > > Then the memorized entry will just hang around forever? > > If so, then how do we do it without polluting the instance var space > (as in example 2), but still get the garbage collection (as in example > 1)? I would live with polluting the instance var space. Btw, I usually do not use memoize but rather an explicit Hash with default_proc. That way I have control over where I place the Hash and hence how long it lives. Of course you can hack something together using #object_id and an external Hash but then you also need to create a finalizer and soon things get overly complicated. If you have issues with polluting the instance variable namespace you can a) put all memoize containers into a single instance variable (typically a Hash) and / or b) generate instance variable names which are likely to be conflict free. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/