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.
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.
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.
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. 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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With