Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveRecord objects in hashes aren't garbage collected -- a bug or a sort of caching feature?
    primarykey
    data
    text
    <p>I have a simple ActiveRecord model called <code>Student</code> with 100 records in the table. I do the following in a rails console session:</p> <pre><code>ObjectSpace.each_object(ActiveRecord::Base).count # =&gt; 0 x = Student.all ObjectSpace.each_object(ActiveRecord::Base).count # =&gt; 100 x = nil GC.start ObjectSpace.each_object(ActiveRecord::Base).count # =&gt; 0 # Good! </code></pre> <p>Now I do the following:</p> <pre><code>ObjectSpace.each_object(ActiveRecord::Base).count # =&gt; 0 x = Student.all.group_by(&amp;:last_name) ObjectSpace.each_object(ActiveRecord::Base).count # =&gt; 100 x = nil GC.start ObjectSpace.each_object(ActiveRecord::Base).count # =&gt; 100 # Bad! </code></pre> <p>Can anyone explain why this happens and whether there is a smart way to solve this without knowing the underlying hash structure? I know I can do this:</p> <pre><code>x.keys.each{|k| x[k]=nil} x = nil GC.start </code></pre> <p>and it will remove all Student objects from memory correctly, but I'm wondering if there is a general solution (my real-life problem is wide spread and has more intricate data structures than the hash shown above).</p> <p>I'm using Ruby 1.9.3-p0 and Rails 3.1.0.</p> <p><strong>UPDATE (SOLVED)</strong></p> <p>Per Oscar Del Ben's explanation below, a few ActiveRecord::Relation objects are created in the problematic code snippet (they are actually created in both code snippets, but for some reason they "misbehave" only in the second one. Can someone shed light on why?). These maintain references to the ActiveRecord objects via an instance variable called @records. This instance variable can be set to nil through the "reset" method on ActiveRecord::Relation. You have to make sure to perform this on all the relation objects:</p> <pre><code>ObjectSpace.each_object(ActiveRecord::Base).count # =&gt; 100 ObjectSpace.each_object(ActiveRecord::Relation).each(&amp;:reset) GC.start ObjectSpace.each_object(ActiveRecord::Base).count # =&gt; 0 </code></pre> <p>Note: You can also use Mass.detach (using the <a href="https://github.com/archan937/ruby-mass">ruby-mass</a> gem Oscar Del Ben referenced), though it will be much slower than the code above. Note that the code above does not remove a few ActiveRecord::Relation objects from memory. These seem to be pretty insignificant though. You can try doing:</p> <pre><code>Mass.index(ActiveRecord::Relation)["ActiveRecord::Relation"].each{|x| Mass.detach Mass[x]} GC.start </code></pre> <p>And this would remove some of the ActiveRecord::Relation objects, but not all of them (not sure why, and those that are left have no Mass.references. Weird).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload