Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find memory leak in a Ruby on Rails project

I have a Ruby on Rails project with what seems to be a memory leak. It keeps using more and more memory until it crashes. Dumping the amount of objects per class using ObjectSpace I've found this:

Name                                                              Count
-----------------------------------------------------------------------
String                                                           649476
Hash                                                              59695
Array                                                             39407
ActiveSupport::Multibyte::Codepoint                               19337
FileNode                                                          17134
Time                                                               3391
Regexp                                                             1944
ActionController::Routing::DividerSegment                          1743
Proc                                                               1597
Gem::Version                                                       1545
Class                                                              1503
Gem::Requirement                                                   1479
ActiveRecord::DynamicFinderMatch                                   1021

I believe FileNode is the problem. It's a model. Any ideas how to find where the references to the 17k FileNodes are being kept?

This is using Ruby 1.8.6 and Rails 2.2.0. Upgrading is not an option unfortunately.

like image 903
pupeno Avatar asked Oct 01 '10 12:10

pupeno


People also ask

How to find and fix memory leaks in Ruby?

A Ruby application (on Rails or not), can leak memory — either in the Ruby code or at the C code level. In this section, you will learn how to find and fix such leaks by using tools such as Valgrind. Valgrind is an application for detecting C-based memory leaks and race conditions.

What is a rails memory leak?

At some point in the life of every Rails developer you are bound to hit a memory leak. It may be tiny amount of constant memory growth, or a spurt of growth that hits you on the job queue when certain jobs run.

How do I debug Ruby code in rails?

After entering the debugging session, you can type in Ruby code as you're in a Rails console or IRB. You can also use p or pp command to evaluate Ruby expressions (e.g. when a variable name conflicts with a debugger command). Besides direct evaluation, debugger also helps you collect rich amount of information through different commands.

How to analyze the stack trace in rails?

How to analyze the stack trace. One common task is to inspect the contents of a variable. Rails provides three different ways to do this: The debug helper will return a <pre> tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:


2 Answers

You might want to look at the presentation "Garbage Collection and the Ruby Heap":

http://www.scribd.com/doc/32718051/Garbage-Collection-and-the-Ruby-Heap

Starting from Slide 26 various useful tools (ltrace, bleak_house, memprof etc.) get explained.

like image 131
Michael Kohl Avatar answered Oct 21 '22 01:10

Michael Kohl


I think you'll find Debugging Ruby by Aman Gupta very helpful. He has also been working on finding and fixing memory leaks in Rails 3 so his debugging techniques will most certainly be helpful.

http://www.scribd.com/doc/23548865/Debugging-Ruby

like image 24
rohit.arondekar Avatar answered Oct 21 '22 00:10

rohit.arondekar