From: "ara.t.howard" Date: 2007-09-26T01:49:32+09:00 Subject: Re: ObjectSpace.define_finalizer and memory leaks On Sep 25, 2007, at 10:14 AM, Sylvain Joyeux wrote: >> it seems like you should be able to define a finalizer on any object >> without preventing it from being gc'd! > The only way I am aware of is to use methods ... > ObjectSpace.define_finalizer(obj, &method(:my_finalizer)) > -- > Sylvain Joyeux http://www.laas.fr/ > ~sjoyeux working off of you idea of avoiding any new references whatsoever i came up with this, which is working for me, can others confirm? cfp:~ > cat a.rb class C def self.count c = 0 ObjectSpace.each_object do |object| c += 1 if self === object rescue next end c end end def finalizer_for object_id lambda { p [:finalized, object_id] if $DEBUG } end loop do (2 ** 16).times do c = C.new finalizer = finalizer_for c.object_id c.instance_eval do ObjectSpace.define_finalizer self, &finalizer end end puts "before: #{ C.count }" GC.start puts "after: #{ C.count }" puts end thanks for the inspiration! kind regards. a @ http://drawohara.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama