Issue #10387 has been reported by Andrew Vit.

----------------------------------------
Bug #10387: Overwriting an array variable doesn't release referenced objects
https://bugs.ruby-lang.org/issues/10387

* Author: Andrew Vit
* Status: Open
* Priority: Normal
* Assignee: 
* Category: core
* Target version: 
* ruby -v: ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
I narrowed down the following, but I'm not sure if the problem is mine or a bug:

~~~
require 'objspace'
GC.start

begin
  threads = []
  1000.times do
    threads << Thread.new { Random.new(10) }
  end
  threads.each &:join

  threads = []  # threads.clear
end

GC.start
puts ObjectSpace.count_tdata_objects
~~~

If I overwrite the threads variable with an empty array or nil, then 1000 Threads and 1000 Random objects remain in the heap.

However, if I run Array#clear to release the threads, then everything is released correctly.

I don't think this issue is related to threads necessarily, I was able to make it happen with regular objects, but it looks like you have to run a method on each object in the array, e.g.

~~~
  objects = []
  1000.times do
    objects << Random.new(10)
  end
  objects.each &:rand   # Random objects remain in memory with this line
  objects = []
~~~



-- 
https://bugs.ruby-lang.org/