Somewhere I read that ObjectSpace#each_object can pick up objects that
are abandoned but not yet garbage-collected. It would be a good idea to
check the validity of that before using it in this context. My question
is - How to do that ?
I wrote the code below :…
…
Somewhere I read that ObjectSpace#each_object can pick up objects that
are abandoned but not yet garbage-collected. It would be a good idea to
check the validity of that before using it in this context. My question
is - How to do that ?
Here’s the result of a simple experiment I did. It shows that ObjectSpace#each_object lists objects that have been deleted but not
yet garbage-collected:
include ObjectSpace
class Cat
def initialize(n) @a = []
n.times {|i| @a << {}}
end
end
def nobjects(msg)
puts “\n#{msg}\n”
Note ObjectSpace#each_object enumerates over all
objects reported by ObjectSpace#count_objects.
puts “number of hash objects = #{ObjectSpace.count_objects[:T_HASH]}”
end
n = 10000 # Create this number of empty hashes
nobjects(“at start”)
=> at start
=> number of hash objects = 77
cat = Cat.new(n)
nobjects(“after creating an array of #{n} empty hashes”)