From: ara howard Date: 2008-01-09T14:20:24+09:00 Subject: Re: why does this code leak? On Jan 8, 2008, at 8:45 PM, Jan Dvorak wrote: > It does not leak, objects are just not immediately released by the > GC. Try it > running 1000 times (on local server preferably). as i already posted - i posted the wrong code. here is an even more distilled version: cfp2:~ > cat a.rb # # distilled behaviour from dike.rb # class Class Finalizer = lambda {} def leak_free_finalizer Finalizer end def leaky_finalizer lambda{} end def finalizer %r/leak/ =~ ARGV.first ? leaky_finalizer : leak_free_finalizer end def new *a, &b object = allocate object.send :initialize, *a, &b object ensure ObjectSpace.define_finalizer object, finalizer end end # # the above makes this code leak iff ARGV has "leak" in it # require "yaml" 7.times { GC.start y Array.name => ObjectSpace.each_object(Array){} Array.new } cfp2:~ > ruby a.rb --- Array: 21 --- Array: 49 --- Array: 58 --- Array: 58 --- Array: 58 --- Array: 58 --- Array: 58 cfp2:~ > ruby a.rb leak --- Array: 21 --- Array: 38 --- Array: 54 --- Array: 67 --- Array: 79 --- Array: 91 --- Array: 103 so.... why does installing a static finalizer work ok, but a dynamic one leaks memory!? i'm nice and confused now. a @ http://codeforpeople.com/ -- share your knowledge. it's a way to achieve immortality. h.h. the 14th dalai lama