Ruby Class object garbage collection

后端 未结 4 1422
不思量自难忘°
不思量自难忘° 2021-01-07 17:22

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

4条回答
  •  温柔的废话
    2021-01-07 18:20

    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.

提交回复
热议问题