ActionMailbox uses lots of RAM although not in use - how to remove?

Is there a way to remove ActionMailbox from my Rails app? I’m getting out of memory errors with Rails 6, and derailer shows action_mailbox as the lead memory hog — even though my app doesn’t use it:

TOP: 116.9336 MiB
  rails/all: 62.0156 MiB
    action_mailbox/engine: 35.7305 MiB

When using rails new --skip-action-mailbox it comments out the line require "action_mailbox/engine" in config/application.rb:

So I’d try replacing require "rails/all" with this:

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

Excellent - great idea.

I think this is an issue. I love how Rails gives you everything out-of-the-box, but the price to pay to have actionmailbox already included is too high. 30MB loaded in memory is not an irrelevant amount.

1 Like

Yep, this is a solution for skipping it, but does anybody know why Action Mailbox, in particular, takes so much memory?

This knowledge may be out-of-date, but at one point in time, the reason why ActionMailbox uses so much memory is:

ActionMailbox relies upon the Mail gem to parse emails. The Mail gem parses emails by creating several state machines with Ragel, which works by creating big arrays of integers which is the source of the memory usage.

You can dig into this write-up for more info:

And here is some of the work that looks like it wasn’t completed to reduce the memory usage of mail parsing: address_lists_parser.rb requires a lot of memory (~30 mb) · Issue #1342 · mikel/mail · GitHub

1 Like

We can quickly verify that it does use a lot of memory when there are a lot of Person records being created!

Update: So far, I’ve found no way to prevent it from being loaded. If anyone has a verified solution — not just a theoretical one — I’d love to hear it.

In the meantime, I’ve gotten my app’s RAM use to easily fit on a 1MB instance by limiting calls to asset helpers. :slight_smile: