title: Error 500 causes memory bloat/leak in middleware
body: <p>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:</p>
  
  <p><a href="https://i.stack.imgur.com/eneJl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eneJl.png" alt="enter image description here"></a> </p>
  
  <p>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 (<code>gem '[exception_notification][2]'</code> v. 4.2.1) so I assume it must be it and it is only my interpretation that is wrong. </p>
  
  <p>Edit after comment by Alexis -  I ran:</p>
  
  <pre><code>Rails.configuration.middleware.each do |m|
  puts "use #{m.inspect}"
  end;0
  </code></pre>
  
  <p>which gave the following output about middleware:</p>
  
  <pre><code>use UTF8Cleaner::Middleware
  use Rack::Sendfile
  use ActionDispatch::Static
  use #&lt;ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x0055f8e1dd5a60&gt;
  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
  </code></pre>
  
  <p>where Airbrake and ScoutApm would be the only one related to errors.</p>
  
  <p>My production.rb contains this information:</p>
  
  <pre><code>  config.middleware.use ExceptionNotification::Rack, :email =&gt; {
        :email_prefix =&gt; "[MyApp.se Exception] ",
        :sender_address =&gt; %{"Exception Notifier" &lt;support@myapp.se&gt;},
        :exception_recipients =&gt; %w{me@myapp.se}
      }
  
    # Taken from mailer.rb
    config.action_mailer.delivery_method = :smtp
  
    config.action_mailer.smtp_settings = {
      :enable_starttls_auto =&gt; false,
      :return_response =&gt; true,
      :raise_delivery_errors =&gt; true,
      :address =&gt; 'smtp.myapp.se',
      :port =&gt; 587,
      :domain =&gt; "myapp.se",
      :user_name =&gt; 'mailer@myapp.se',
      :password =&gt; ENV['MAILER_PWD'],
      :authentication =&gt; 'plain'
    } 
  </code></pre>
  
  <p>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. </p>
  
  <p>Why does this happen? How can I solve it, or at least troubleshoot it further? Could it be any other Rails default middleware?</p>