在下面的代碼不工作:紅寶石迫使垃圾收集預期
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
這裏的終結,不會被調用沒有輸出。因爲它已被取消引用,所以我期望垃圾收集器收集ex
。爲什麼GC.start
不會強迫ex
被收集並立即導致finalizer to be called
?
我看到'dead'打印,如果我使用控制關閉您的代碼+ C,以及如果我取代'exit'了'而真正end'作爲。我在Ruby 2.5.0-dev fwiw上。你在哪個平臺上? –
我在eclipse上使用Ruby 2.4.0 – aoiee
哦,並且沒有GC.start的終結器工作? –