Mar 30, 2022 - Salvatore Ferrucci

Adding jemalloc to Rails apps on Heroku

One of the earliest projects I was involved in at Algonauti was investigating ways to optimise performance and memory consumption in our Rails web application.

jemalloc is a general purpose malloc(3) implementation developed by Jason Evans that emphasizes fragmentation avoidance and scalable concurrency support. jemalloc first came into use as the FreeBSD libc allocator in 2005, and since then it has found its way into numerous applications that rely on its predictable behavior.

If you're seeing too much memory use, replace malloc with jemalloc could be a good solution.

Install

To replace malloc on Heroku with jemalloc you can use a third party jemalloc buildpack. Enabling it in a Rails app on Heroku consists of following step.

heroku buildpacks:add --index 1 https://github.com/gaffneyc/heroku-buildpack-jemalloc.git

Usage

Set the JEMALLOC_ENABLED config option to true and jemalloc will be used for all commands run inside of your dynos.

heroku config:set JEMALLOC_ENABLED=true

Set JEMALLOC_VERSION to select or pin to a specific version of jemalloc, if it is not set the default is to use the latest stable version. You will receive an error mentioning tar if the version does not exist.

note: This setting is only used during slug compilation. Changing it will require a code change to be deployed in order to take affect.

heroku config:set JEMALLOC_VERSION=3.6.0

Conclusion

With just enabling jemalloc, we had incredible, tangible performance and memory consumption benefits on Heroku without doing any code change. Since picking it up, we no longer see the steady RSS growth in our app.

This post only encompasses part of what we ended up tuning and analyzing to get the most out of our systems resources, though. Keep an eye out for more posts to follow!

Adding jemalloc to Rails apps on Heroku via @safeforge

Click to tweet