Project

General

Profile

Bug #19460

Updated by luke-gru (Luke Gruber) about 1 year ago

I'm working on something where I need to remove a class and release all memory related to that class. 
 I've stumbled upon a limitation where in some instances I cannot get the class to be GC'd. The problem 
 (I think) is that inline method caches cache the class, and even if the class would otherwise be gone, it stays. 
 For example: 

 ```ruby 
 class A 
   def do_something 
   end 
 end 
 a = A.send(:new) 
 a_id = A.object_id 
 a = nil 
 Object.send(:remove_const, :A) 

 # A should be able to be released now. 

 10.times { GC.start } 
 a_ref = ObjectSpace._id2ref(a_id) rescue nil 
 puts "a_ref: #{a_ref.class}" 

 # we get NilClass, it is released 
 ``` ```ruby 

 However: using `A.new` above, it can't be GC'd. 
 If there is an initialize method (even empty) it can't be GC'd. 
 If there's a method call inside the class to another instance method on self ex: `self.do_something`, it can't be GC'd. 

 I'm not sure exactly what is going on but it looks like an inline cache marking issue. Maybe caches are getting marked, even inside methods where the method itself doesn't get marked.

Back