Project

General

Profile

Bug #10387

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

I narrowed down the following, but I'm not sure if the problem is mine or a bug: 

 ~~~ruby ~~~ 
 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. 

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

Back