【发布时间】:2017-01-28 15:42:36
【问题描述】:
我是 Ruby 新手,所以如果我遗漏了什么,我深表歉意。我有一个类启动一个线程来执行一些内部缓存刷新。即使我加入线程并将其设置为零,然后将所有类实例设置为零,该类也不会被释放。有没有人看到这个问题?我究竟做错了什么? 谢谢 丹尼尔
我的复制人:
class KK
def initialize()
@thread = Thread.new(&method(:refresh_cache))
ObjectSpace.define_finalizer( self, self.class.finalize() )
end
def self.finalize()
proc {
puts 'Object KK Disposed'
}
end
def wait_thread
puts 'join thread'
@thread.join #also tried with exit and kill
@thread=nil
end
def refresh_cache
puts 'KK still here'
end
end
puts 'started'
puts Thread.list
k2=KK.new
puts ObjectSpace.each_object(KK).count
puts Thread.list
k2.wait_thread
k2=nil
puts 'After nil'
puts Thread.list
GC.start
sleep 5
#still one instance here
puts ObjectSpace.each_object(KK).count
【问题讨论】:
标签: ruby multithreading garbage-collection