Nested include has major memory leak (Rails 2.0.1).

Posted by Bart ten Brinke Wed, 02 Jul 2008 14:00:33 GMT

As our mongrels were using up quite a lot of memory, so I tried to figure out what was causing this.

When running the app locally I found out that one certain page caused the mongrel to grow from 60 to 190 megabytes. A whopping 130 megabytes!

After commenting out some of the code, I realized that a single line was causing all of the memory usage

contracts = Contract.find( :all, 
  :conditions => ['contracts.employee_id IN (?) ', employees ],
  :include => [:expertise_profile => :qualifications ] )

Ouch! The nested include of rails somehow leaks a large amount of memory. The fix was a piece of cake.

contracts = Contract.find( :all, 
  :conditions => ['contracts.employee_id IN (?) ', employees ],
  :include => :expertise_profile )

Now my mongrel stays at 60 megabytes. I don't know if this issue persists in the new 2.1 Rails, but I'll check that soon!

Comments

  1. Jon Dahl said about 1 hour later:
    Great catch. Be sure to post again if you find out anything more (like: if 2.1 has the same problem, or the underlying cause of the leak).
  2. Steve Root said 34 days later:
    I'm new to rails, but isn't the difference here that you are no longer (eager) loading the qualifications table into the model? The less data you load, the smaller the memory used
  3. Bart ten Brinke said 43 days later:
    You are correct Steve, but normally after a request, the mongrel should return to its normal memory footprint, which in this case it does not. The footprint grew 130 MB which is only returned when you restart the mongrel.
  4. Matt Griffith said 44 days later:
    Thanks for posting this. I'm seeing the same thing even when using a non-nested :include. I have an :include that loads 6 associations and it has a similar leak. This is in 2.0.2, I haven't tested 2.1 yet.

(leave url/email »)

   Preview comment