DEV Community

Prathamesh Sonpatki
Prathamesh Sonpatki

Posted on • Originally published at prathamesh.tech on

1 1

Adding jemalloc to Rails apps on Heroku

jemalloc is a malloc implementation developed by Jason Evans which is known to improve memory consumption of Rails apps, especially on Heroku. By default Ruby uses malloc from C to manage memory but it can run into memory fragmentation issues.

Whereas jemalloc describes itself as a malloc implementation which tries to avoid memory fragmentation. A lot of people have successfully tested jemalloc in production apps deployed on Heroku to verify that it reduces memory usage compared to the default memory management that comes with Ruby.

Enabling it in a Rails app on Heroku consists of following steps.

Add jemalloc buildpack

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

Enable jemalloc

This can be done in two ways. Either we can set the environment variable, JEMALLOC_ENABLED to true.

heroku config:set JEMALLOC_ENABLED=true
Enter fullscreen mode Exit fullscreen mode

Alternatively, we can add jemalloc.sh prefix to the processes listed in the Procfile.

# Procfile

web: jemalloc.sh bin/puma -C config/puma.rb
worker: jemalloc.sh bundle exec sidekiq -q default -q mailers -c ${SIDEKIQ_CONCURRENCY:-5}
Enter fullscreen mode Exit fullscreen mode

Note that setting the JEMALLOC_ENABLED environment variable will enable jemalloc for all processes of your app, where as adding jemalloc.sh prefix in Procfile can give you control over for which processes you want to enable it.

After this is done, deploying the app on Heroku will enable jemalloc and we can monitor the memory consumption.

jemalloc version

We can choose the jemalloc version by setting JEMALLOC_VERSION to a version number from this list. By default, the buildpack chooses the most recent version.

Conclusion

With just enabling jemalloc, we can see significant drop in memory usage on Heroku without doing any code change. So I highly recommend enabling it in production apps deployed on Heroku.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay