title: Tracing memory leak in Ruby on Rails 3 / Postgres / Apache Passenger application
body: <h2>Hello,</h2>
  
  <p>we have recently updated an application to Rails 3.0.4 (3.0.5 on online devel server). Most of the changes from 2.3.10 to 3.0.4 were due to obsolete or outdated plugins and gems, and were solvable with relative ease. But one thing makes me go mad:</p>
  
  <p>Every single web request, in development mode, causes the server process to allocate about 50-60 MB <strong>more memory</strong> than before. This memory is <strong>not</strong> freed after the request, at least not all of it. After 10-20 requests, every Ruby instance consumed over <strong>500 MB of RAM</strong>, while our previous Rails 2.3.10 instances were rarely above 200 MB.</p>
  
  <p>This makes it impossible to run our 1300 tests, because the devel machine's 4GB of RAM is filled before the end of the tests. It only happens in development mode with <code>cache_classes = false</code>. If I switch cache_classes to true, the Rails instances will consume about 200MB of memory, and then stay there. However, during tests, even with cache_classes = true, memory usage will grow.</p>
  
  <p>I queried ObjectSpace and found out that with each request, about 3500 new Proc, up to 50'000 new Strings and 3000 new Hashes and Arrays are created and not freed. These strings (when dumped) contained my whole source code including plugins and gems, documentation, source code comments and path names. (Why?)</p>
  
  <p>To find the cause for this, here's what I tried: (After every change, I hammered the apps with <code>ab -n 50</code>.)</p>
  
  <ol>
  <li>I created a <strong>fresh Rails 3 application</strong> with a single resource and controller and SQLite3 DB. <em>Memory usage started at 60 MB and stayed below 80 MB</em>.</li>
  <li>I changed <strong>'sqlite3' to 'pg'</strong> and pointed the new Rails 3 app to my existing Postgres DB. <em>Memory usage started at 110 MB and did not grow beyond 130MB</em>. (Side question: Why does the Postgres gem use so much more memory than the SQLite3 gem?)</li>
  <li>I copied over my <strong>Gemfile and Gemfile.lock</strong> from the broken Rails3 app to the bare bones app and ran <em>bundle install</em>. <em>No change</em>, memory stayed at about 115MB, no matter how many requests were made.</li>
  <li>I created an empty "def FooController; def foo; render :text => 'foo' end; end" in the broken Rails3 app. <em>Memory usage grew more slowly, but it still never stopped growing after requests</em>. </li>
  <li>I deleted every route except for the FooController route. <em>No change</em>.</li>
  <li>I disabled all Gems except for the following: <code>pg, rails, aasm, will_paginate, geokit-rails3, koala, omniauth, paperclip</code>. <em>No change</em>.</li>
  <li>I disabled every before_filter and after_filter in ApplicationController and every nonessential <code>include</code> in environment.rb. I also synced boot.rb, environment.rb and application.rb with my bare-bones Rails 3 app, except for five relatively simple observers, autoloading files in /lib and filter_parameters. <em>No change</em>. Every new request still consumed an additional 10-50 MB of RAM.</li>
  </ol>
  
  <p>If you have an idea what is going wrong here, and where the memory leak could be, I would really appreciate any help. I am running Rails 3.0.4 on OS X Snow Leopard, Rails 3.0.5 on Debian Lenny, and </p>
  
  <p>Thank you!</p>
  
  <h2>Coming closer:</h2>
  
  <p>I have removed every plugin, every gem, every extension and everything that I did not personally write myself, so that my application is basically naked. Especially, I removed these plugins: <code>acts_as_list, acts_as_tree, asset_packager, forgot_password, fudge_form, fudge_scaffold, paperclippolymorph, query_trace, rails_upgrade, repeated_auto_complete-0.1.0, role_requirement, to_select, validates_url, and ym4r_gm</code>.</p>
  
  <p>Now my application - only the above FooController still works! - starts up with 65MB and never goes beyond 75MB of RAM, even after hammering it with <code>ab -n 1000 -c1</code> (1000 HTTP requests to /foo using ApacheBench). Unfortunately, without the plugins, this is also the only URI that works at all.</p>
  
  <p>After some digging, it seems that a combination between the Restful Authentication and Acts As State Machine (AASM) plugins causes the memory leak. See also <a href="https://github.com/Satish/restful-authentication/issues#issue/11" rel="noreferrer">https://github.com/Satish/restful-authentication/issues#issue/11</a>. I'm not sure yet why, and just doing "include AASM" in my bare-bones project does not cause RAM usage growth just by itself.</p>
  
  <p>I will investigate further.</p>
  
  <h2>Culprit found</h2>
  
  <p>It is AASM. In Rails 3 it seems to leak AASM::xxx object instances. see </p>
  
  <ul>
  <li><a href="https://github.com/jeffp/enumerated_attribute/issues/#issue/20" rel="noreferrer">https://github.com/jeffp/enumerated_attribute/issues/#issue/20</a></li>
  <li><a href="https://github.com/rubyist/aasm/issues/31" rel="noreferrer">https://github.com/rubyist/aasm/issues/31</a></li>
  <li><a href="https://github.com/Satish/restful-authentication/issues/#issue/11" rel="noreferrer">https://github.com/Satish/restful-authentication/issues/#issue/11</a></li>
  </ul>
  
  <h2>Second culprit found</h2>
  
  <p>There was another memory leak in rspec. This made my tests almost unbearably slow, even after removing AASM, because two parallel running rspec tasks (using <a href="https://github.com/grosser/parallel_tests" rel="noreferrer">https://github.com/grosser/parallel_tests</a>) took almost 3GB of memory at the end. See <a href="https://github.com/rspec/rspec-core/issues/#issue/321" rel="noreferrer">https://github.com/rspec/rspec-core/issues/#issue/321</a>. </p>