On Sep 17, 2008, at 8:49 AM, Brian Candler wrote:

> Your version only works because the threads are dying after 2 seconds.
> Change
>
>  def make_thread
>    Thread.new { sleep 2 }
>  end
>
> to
>
>  def make_thread
>    Thread.new { sleep 10000 }
>  end
>
> and run it again.
>
> The big arrays are happily garbage-collected; the Foos are not.
> --  
> Posted via http://www.ruby-forum.com/.
>


but that it expected?  you have a Foo which referes to a Thread which  
has not died that is itself referred to by the global Thread.list

cfp:~ > ruby -e' 41.times{ Thread.new{ sleep } }; p Thread.list.size '
42


so if they when the threads die, the Foos are freed


cfp:~ > cat a.rb
require 'yaml'

def make_thread
   Thread.new { sleep 2 }
end

class Foo
   attr_accessor :bar
end


n = Integer(ARGV.first || 1024)

3.times do
  info = {}

  n.times { |i|
    f = Foo.new
    f.bar = make_thread
  }

  info.update 'before' => ObjectSpace.each_object(Foo){}

  sleep 2.42
  GC.start

  info.update 'after' => ObjectSpace.each_object(Foo){}

  y info
end


cfp:~ > ruby a.rb
---
after: 2
before: 1024
---
after: 2
before: 1026
---
after: 2
before: 1026




a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama