Ruby Class对象垃圾收集 [英] Ruby Class object garbage collection
本文介绍了Ruby Class对象垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
一个更具体的例子,类似于Andrew Cholakian的答案是使用ObjectSpace。例如:
2.1.1:001> ObjectSpace.count_objects [:T_CLASS]
=> 884
2.1.1:002> 10000.times {Class.new}
=> 10000
2.1.1:003> ObjectSpace.count_objects [:T_CLASS]
=> 20884
2.1.1:004> GC.start
=>无
2.1.1:005> ObjectSpace.count_objects [:T_CLASS]
=> 884
这表明匿名类(不是保存在任何地方的常量中,或者被这些类的任何实例使用)确实收到垃圾。
In ruby all classes are objects of class Class. Since classes are also objects, does a Ruby VM follow the same Garbage Collection strategy for class objects? What determines that a class object is safe for garbage collection?
解决方案
An even more concrete example, similar to Andrew Cholakian's answer is to use ObjectSpace. For example:
2.1.1 :001 > ObjectSpace.count_objects[:T_CLASS]
=> 884
2.1.1 :002 > 10000.times { Class.new }
=> 10000
2.1.1 :003 > ObjectSpace.count_objects[:T_CLASS]
=> 20884
2.1.1 :004 > GC.start
=> nil
2.1.1 :005 > ObjectSpace.count_objects[:T_CLASS]
=> 884
This shows that anonymous classes (not saved in a constant anywhere or used by any instances of those classes) do indeed get garbage collected.
这篇关于Ruby Class对象垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文