In the following code:
class ExampleClass
def initialize
ObjectSpace.define_finalizer(self, proc{puts 'dead'})
end
end
ex = ExampleClass.new
ex = nil
GC.start
while true
# the while loop is to prevent the program from terminate
# if the program does terminate, the finalizer gets called in the end like expected
end
The finalizer here is never called and there is no output. I would have expected the garbage collector to collect ex since it has been dereferenced. Why is GC.start not forcing ex to be collected and causing the finalizer to be called immediately?
deadprint if I close your code using control+c as well as if I replace thewhile true endwithexit. I'm on Ruby 2.5.0-dev fwiw. What platform are you on? - max pleanerdeaddoes not print right when I callGC.start. I put a while loop there to show that the program does not terminate. - aoiee