I recently discovered that I get memory bloats when I experience an error code 500 in my Rails apps (I run several and experience the same in all). I found out using Scout and could see a pattern of memory leaks (almost) every time I had a 500-error. Below is the most recent example:
Although it seems in the graph that exception_notification does not have much memory allocation, but rather "Middleware" has. The only middleware I use (as far as I know) is the Exception Notification (gem '[exception_notification][2]'
v. 4.2.1) so I assume it must be it and it is only my interpretation that is wrong.
Edit after comment by Alexis - I ran:
Rails.configuration.middleware.each do |m|
puts "use #{m.inspect}"
end;0
which gave the following output about middleware:
use UTF8Cleaner::Middleware
use Rack::Sendfile
use ActionDispatch::Static
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x0055f8e1dd5a60>
use Rack::Timeout
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Callbacks
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use Airbrake::Rack::Middleware
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use ExceptionNotification::Rack
use Rakismet::Middleware
use ScoutApm::Middleware
where Airbrake and ScoutApm would be the only one related to errors.
My production.rb contains this information:
config.middleware.use ExceptionNotification::Rack, :email => {
:email_prefix => "[MyApp.se Exception] ",
:sender_address => %{"Exception Notifier" <[email protected]>},
:exception_recipients => %w{[email protected]}
}
# Taken from mailer.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => false,
:return_response => true,
:raise_delivery_errors => true,
:address => 'smtp.myapp.se',
:port => 587,
:domain => "myapp.se",
:user_name => '[email protected]',
:password => ENV['MAILER_PWD'],
:authentication => 'plain'
}
As I have been on a Great Memory Bloat Hunt for the last week I have read that mail at least used to be a cause for concern so there may be something about this.
Why does this happen? How can I solve it, or at least troubleshoot it further? Could it be any other Rails default middleware?
IMO a better way to send notifications in rails is via the airbrake gem. You could run an instance of errbit to save some money or sign up to use airbrake itself. This also has the benefit of giving you a lot more flexibility in how you receive the notifications too, e.g. pushover, and you'll receive full stack traces that you can act upon vs having to hunt through the logs.