Ruby F.s,
I’m having trouble understanding when Ruby GC kicks in for Object
instances that are no longer referred to by anything. A simple code
sample to illustrate:
class TestClass
end
collection = []
collection << TestClass.new
collection << TestClass.new
collection << TestClass.new
collection << TestClass.new
numTC = 0
ObjectSpace.each_object{|obj| numTC += 1 if obj.class == TestClass }
puts numTC
collection.delete_at(0)
collection.delete_at(0)
collection.delete_at(0)
collection.delete_at(0)
numTC = 0
ObjectSpace.each_object{|obj| numTC += 1 if obj.class == TestClass }
puts numTC
If you run this you’ll see that 4 TestClass instances are still in the
ObjectSpace even after they are deleted from the collection array. I
tried manually calling the garbage collector after deleting the elements
from the array but I get the same result.
How do I get Ruby to trash those instances?
Thanks,
Mark