From: Robert Klemme Date: 2005-04-13T02:59:36+09:00 Subject: Re: ability to run finalizers at a given point of a program? "Guillaume Cottenceau" schrieb im Newsbeitrag news:dc3bf8580504120943240e01e4@mail.gmail.com... > Hi, > > I'm considering the possibility to run the finalizers at a given point > of a program. I have written the following program, and I'm > experiencing the following unexpected behaviour: I call GC.start in > the hope that finalizers of unscoped objects that have one will be > run, but it seems they aren't. > > http://www.zarb.org/~gc/t/prog/destructor/nodestructor.rb Apart from what Guy wrote already, why do you need to determine the point in time when finalizers are called? The whole idea of GC and finalization is that you *don't* care when it happens. If you need to ensure (hint, hint) that some cleanup code is invoked at some point in time then the transactional pattern employed by File.open() and others might be more appropriate: def do_work x = create_x_somehow begin yield x ensure # always called, even in case of exception x.cleanup end end do_work do |an_x| puts an_x.to_u end And another remark: as opposed to Java finalizers, Ruby finalizers are guaranteed to be invoked, even on program exit: $ ruby -e 'ObjectSpace.define_finalizer(Object.new){ puts "called" }' called $ ruby -e 'o=Object.new;ObjectSpace.define_finalizer(o){ puts "called" }' called Btw, you can also define exit handlers with at_exit: http://www.ruby-doc.org/core/classes/Kernel.html#M001736 Kind regards robert