在Ruby on Rails项目中查找内存泄漏

问题描述:

我有一个Ruby on Rails项目,似乎是内存泄漏.它一直使用越来越多的内存,直到崩溃.使用ObjectSpace转储每个类的对象数量,我发现了这一点:

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

我相信FileNode是问题所在.这是一个模型.有什么想法可以找到保存17k FileNode引用的位置吗?

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?

这是使用Ruby 1.8.6和Rails 2.2.0.不幸的是,升级不是一种选择.

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

Charles Oliver"Headius" Nutter 最近写了一系列有关使用JVM工具调试Ruby中的内存泄漏的博客文章:

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,JRuby社区的其他成员在同一时间附近也有关于该主题的其他一些博客文章.

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.

他们的基本论点(尽管他们礼貌得不能这么说)是使用JRuby以外的任何其他工具来调试内存泄漏只是愚蠢的,这仅仅是因为JRuby可以使用投入了更多工程工作的Java工具比所有 Ruby分析工具 更好. Ruby社区免费获得了这些工具,因为所有企业Java无人机都在为它们付费.

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.