zombocom / derailed_benchmarks

Go faster, off the Rails - Benchmarks for your whole Rails app
2.97k stars 144 forks source link

Memory Leak Issue with derailed and Rails Configuration #257

Open gatopanx opened 1 month ago

gatopanx commented 1 month ago

First and foremost, thank you for this outstanding tool! It has been incredibly helpful in identifying memory issues, and I truly appreciate the work that’s gone into it.

While debugging some memory leaks, I happened to find a discrepancy that I wanted to bring to your attention. When I ran:

bundle exec derailed bundle:mem

I noticed that a nested dependency was significantly increasing memory usage on boot, even with the default Rails configuration:

   1 TOP: 176.2227 MiB
   2   rails/all: 58.1875 MiB
   3     action_mailbox/engine: 31.2695 MiB
   4       action_mailbox: 31.2539 MiB
   5         action_mailbox/mail_ext: 31.25 MiB
   6           action_mailbox/mail_ext/address_equality.rb: 28.8984 MiB
   7             mail/elements/address: 28.8945 MiB
   8               mail/parsers/address_lists_parser: 28.8672 MiB
   9           mail: 2.3516 MiB
  10             mail/field: 0.5234 MiB
  11             mail/message: 0.4102 MiB

To address this, I adjusted config/application.rb to load only the required components:

require_relative 'boot'

require 'rails'
require 'active_model/railtie'
require 'active_job/railtie'
require 'active_record/railtie'
require 'active_storage/engine'
require 'action_controller/railtie'
require 'action_mailer/railtie'
# require 'action_mailbox/engine'
require 'action_text/engine'
require 'action_view/railtie'
require 'action_cable/engine'
require 'sprockets/railtie'
require 'rails/test_unit/railtie'

# ...other configurations

However, upon re-running bundle exec derailed bundle:mem, the memory consumption results remained unchanged. After investigating further, I found that derailed appears to load the full Rails stack regardless of the configuration in config/application.rb. The relevant code is here: https://github.com/zombocom/derailed_benchmarks/blob/main/bin/derailed#L90.

This behavior may not align with users’ expectations, especially when debugging memory issues, as it could lead to a mismatch between actual application memory usage and what derailed reports. I understand that the documentation does mention that certain components (e.g., ActiveRecord) can be skipped, but the current behavior could be misleading when attempting more granular memory optimization.

Would it be possible to consider an enhancement that allows derailed to respect the customized Rails boot process, or to clarify this behavior in the documentation?

Thank you again for all your efforts on this project! Happy to assist further if needed.

Best, Ivan

gatopanx commented 1 month ago

Related: https://stackoverflow.com/questions/64364178/rails-6-how-to-disable-action-mailbox-and-action-text-from-loading-into-memory