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
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
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}
Note that setting the JEMALLOC_ENABLED environment variable will enable jemalloc for all processes of your app, where as adding
jemalloc.shprefix 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.
Top comments (0)