Satish / restful-authentication

Generates common user authentication code for Rails/Merb, with a full test/unit and rspec suite and optional Acts as State Machine support built-in.
http://weblog.techno-weenie.net
MIT License
82 stars 19 forks source link

Huge memory hole with restful_auth in Rails 3 #11

Open jensb opened 13 years ago

jensb commented 13 years ago

I'm experiencing a huge memory leak issue in Rails 3.0.4 and 3.0.5. Both on WEBrick and Apache Passenger 3.0.3, both on OS X and Linux (Debian Lenny), using Ruby 1.8.7.

I had a Rails 2.3.10 application running with restful_auth, and upgraded it to 3.0.4. This is when the leak started. See also http://stackoverflow.com/questions/5369623/tracing-memory-leak-in-ruby-on-rails-3-postgres-apache-passenger-application.

class ApplicationController < ActionController::Base
  include AuthenticatedSystem
  include RoleRequirementSystem
  ...
end

With these two includes, "ab -n 1000 -c1" (Apachebench, 1000 requests) will make my WEBrick process grow from 60 MB to about 600-800 MB. This also happens in tests, because of the fact that the tests won't run when I don't use the above includes in test_helper.rb. (Why not?)

Without these two includes, the same apachebench will only make it grow about 20MB.

I suspect the two included modules are somehow implicitly preloading User, and doing this again on every request. User is (in my case) the object which should be authenticable, and fairly large, with about 50 associations; it probably triggers another heap of objects being preloaded.

Is there a way to avoid this behaviour?

Thanks!

jensb commented 13 years ago

I dug further and identified AASM (Acts As State Machine) as the probable cause for this. When I put "gem 'aasm'" in my Gemfile and "include AASM" in a model, the memory usage grows with each request, it seems, depending on how complex your state definitions are. See here: https://github.com/rubyist/aasm/issues/31

jensb commented 13 years ago

BTW: this only happens when cache_classes == false. (i.e. in development mode)