On 23.04.2007 22:47, Wincent Colaiuta wrote:
> On 23 abr, 20:49, Robert Klemme <shortcut... / googlemail.com> wrote:
>> Maybe you leak only under certain conditions.  Dunno how complex your
>> code in parse is but this seems to be an option.  Maybe you can print
>> more along with the cache's id so as to get an idea in which cases you
>> don't see finalization.
> 
> I'll do that.
> 
> One thing which is puzzling me in the meantime is why GC doesn't seem
> to work in the following example:
> 
> # start example
> 
> def finalizer(thing)
>   puts "setting up finalizer for #{thing}"
>   lambda { puts "finalizer for #{thing}" }
> end
> 
> def meth
>   foo = "hello, world!"
>   ObjectSpace.define_finalizer(foo, finalizer(foo.object_id))
> end
> 
> puts "about to call meth"
> meth
> puts "did call meth"
> 
> puts "about to start GC"
> GC.start
> puts "did start GC"
> def finalizer(thing)
>   puts "setting up finalizer for #{thing}"
>   lambda { puts "finalizer for #{thing}" }
> end
> 
> def meth
>   foo = "hello, world!"
>   ObjectSpace.define_finalizer(foo, finalizer(foo.object_id))
> end
> 
> puts "about to call meth"
> meth
> puts "did call meth"
> 
> puts "about to start GC"
> GC.start
> puts "did start GC"
> 
> # end example
> 
> On my system the output:
> 
> about to call meth
> setting up finalizer for 1005330
> did call meth
> about to start GC
> did start GC
> finalizer for 1005330
> 
> I would have thought that the explicit GC would cause foo's finalizer
> to be called immediately, but no, it only gets called just before the
> process exits.

You have wrong expectations about GC.  Whenever a garbage collected 
language offers explicit start of GC there is usually no guarantee that 
actually all space will be reclaimed at that time.  It's rather a hint 
to the GC that it might be a good idea to start collecting.  Whether 
that actually happens and how much is collected is completely at the 
discretion of the GC's implementation.

> This isn't the only case in which GC doesn't perform as I would
> expect... As another example, the WeakRef code listing on page 732 of
> the Pickaxe doesn't work either. It's supposed to print:
> 
> Initial object is fol de rol
> Weak reference is fol de rol
> prog.rb:8: Illegal Reference - probably recycled (WeakRef::RefError)
> 
> But for me it prints:
> 
> Initial object is fol de rol
> Weak reference is fol de rol
> Initial object is fol de rol
> 
> So either something is awry with the listing, with my understanding of
> GC, or with my local Ruby install (1.8.6 on Mac OS X 10.4.9).

The second option. :-)

	robert