Save RAM by running Mastodon with jemalloc

If you’re running your on Mastodon instance you probably might not have jemalloc active on your Ruby setup. Jemalloc changes the way Ruby allocates memory and saves up RAM. On my 140 users instance RAM usage dropped by 300 MB just by using jemalloc.

This is how to set it up on Debian 9 Stretch:

As root, install libjemalloc-dev:

apt install libjemalloc-dev

Change to mastodon user:

su - mastodon

Check your active ruby version and if there is a new version recommended in the Mastodon Production Guide (see rbenv install command). At the time of writing my server was running on Ruby 2.5.0, but version 2.5.1 was recommended. Therefore I did not recompile the old Ruby version, but the new one instead. If you are on the recommended version, just build the same version again.

RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.5.1

Make sure the latest Ruby version in active:

rbenv global 2.5.1

Leave mastodon user and restart all the services:

exit
systemctl restart mastodon-sidekiq
systemctl restart mastodon-streaming
systemctl restart mastodon-web

Done! Your Mastodon instance should now be less hungry for RAM.