Ken Simon <ninkendo / gmail.com> wrote:
> It appears that running an array through .map(&:foo) leaks the array's
> contents, and they don't get picked up by the Garbage Collector.
> 
> Given a simple class:
> 
>  class C
>    def foo
>     "foo"
>    end
>  end
> 
> The following appears to leave references around (1.9.3-preview1 irb session shown, ruby -v gives ruby -v
> ruby 1.9.3dev (2011-07-31 revision 32789) [x86_64-darwin11.1.0]):
> 
>  ruby-1.9.3-preview1 :001 > a = 10.times.map{C.new}
>   => [... snip ...]
>  ruby-1.9.3-preview1 :002 > b = a.map(&:foo)
>   => ["foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo"] 
>  ruby-1.9.3-preview1 :003 > a = b = nil
>   => nil 
>  ruby-1.9.3-preview1 :004 > GC.start
>   => nil 
>  ruby-1.9.3-preview1 :005 > ObjectSpace.each_object(C){}
>   => 10

GC never guarantees objects will be freed at any determined point in
time.

The only way to prove a leak in the GC is to have an infinite loop and
watch for unbounded memory growth (I watch the process in "top"):

	loop do
	  a  = 10.times.map{C.new}
	  b = a.map(&:foo)
	end