From: ts Date: 2007-04-24T18:39:23+09:00 Subject: Re: Tracking down a garbage collection problem >>>>> "W" == Wincent Colaiuta writes: W> One thing which is puzzling me in the meantime is why GC doesn't seem W> to work in the following example: Well, you must understand that the GC is conservative, this mean that it can mark object which are not used any more by ruby, and more it work at assembler level (sort of) when you look at ruby level. Here your example on *my* system moulon% cat b.rb #!/usr/bin/ruby 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 meth puts "about to start GC" GC.start puts "did start GC" meth puts "about to start GC" GC.start puts "did start GC" moulon% moulon% ./b.rb setting up finalizer for -605724018 about to start GC did start GC setting up finalizer for -605722868 about to start GC did start GC finalizer for -605724018 finalizer for -605722868 moulon% When the GC run, it find the 2 objects on the C stack and mark it, now if it write it like this moulon% diff -u b.rb~ b.rb --- b.rb~ 2007-04-24 11:34:04.000000000 +0200 +++ b.rb 2007-04-24 11:34:21.000000000 +0200 @@ -9,12 +9,12 @@ ObjectSpace.define_finalizer(foo, finalizer(foo.object_id)) end -meth +1.times { meth } puts "about to start GC" GC.start puts "did start GC" -meth +1.times { meth } puts "about to start GC" GC.start puts "did start GC" moulon% moulon% ./b.rb setting up finalizer for -605810148 about to start GC did start GC setting up finalizer for -605808898 about to start GC finalizer for -605810148 did start GC finalizer for -605808898 moulon% and worst moulon% diff -u b.rb~ b.rb --- b.rb~ 2007-04-24 11:34:04.000000000 +0200 +++ b.rb 2007-04-24 11:35:15.000000000 +0200 @@ -9,12 +9,12 @@ ObjectSpace.define_finalizer(foo, finalizer(foo.object_id)) end -meth +1.times { 1.times { meth } } puts "about to start GC" GC.start puts "did start GC" -meth +1.times { 1.times { meth } } puts "about to start GC" GC.start puts "did start GC" moulon% moulon% ./b.rb setting up finalizer for -605462098 about to start GC finalizer for -605462098 did start GC setting up finalizer for -605461048 about to start GC finalizer for -605461048 did start GC moulon% I've just made in sort to call #meth with enough stack to make in sort that the GC don't find the variable in the stack.when it run. But remember this is on *my* system and it can give different results on *your* system and *this is NOT important* Guy Decoux