Project

General

Profile

Bug #10387

Overwriting an array variable doesn't release referenced objects

Added by avit (Andrew Vit) about 5 years ago. Updated 4 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]
[ruby-core:65712]

Description

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 = []

History

Updated by nobu (Nobuyoshi Nakada) about 5 years ago

  • Description updated (diff)

Seems it doesn't happen on the trunk.

#2

Updated by jeremyevans0 (Jeremy Evans) 4 months ago

  • Status changed from Open to Closed

Also available in: Atom PDF