开发者

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 o开发者_如何学JAVAf 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.


Charles Oliver "Headius" Nutter has recently written a series of blog posts on debugging memory leaks in Ruby using JVM tools:

  • Browsing Memory the JRuby Way
  • Finding Leaks in Ruby Apps with Eclipse Memory Analyzer
  • Browsing Memory with Ruby and Java Debug Interface
  • Monitoring Memory with JRuby, Part 1: jhat and VisualVM
  • Monitoring Memory with JRuby, Part 2: Eclipse Memory Analyzer

IIRC, there were also a couple of other blog posts on that same topic by other members of the JRuby community around the same time.

Their basic argument (although they are too polite to spell it out that way) is that using anything other than JRuby to debug memory leaks is just plain stupid, simply because JRuby can use Java tools which have more engineering effort put into them than all Ruby profiling tools together. And the Ruby community gets those tools for free, because all the enterprise Java drones are paying for them.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜