Skip to content

Instantly share code, notes, and snippets.

@ricsdeol
Forked from jjb/file.md
Created May 7, 2024 21:46
Show Gist options
  • Save ricsdeol/6d646f780f40ee408aa4ad47e3aea975 to your computer and use it in GitHub Desktop.
Save ricsdeol/6d646f780f40ee408aa4ad47e3aea975 to your computer and use it in GitHub Desktop.
Using Jemalloc 5 with Ruby.md

For years, people have been using jemalloc with ruby. There were various benchmarks and discussions. Legend had it that Jemalloc 5 didn't work as well as Jemalloc 3.

Then, one day, hope appeared on the horizon. @wjordan offered a config for Jemalloc 5.

Ubuntu/Debian

FROM ruby:3.1.2-bullseye
RUN apt-get update ; \
    apt-get install -y --no-install-recommends libjemalloc2 ;
ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2
ENV MALLOC_CONF='dirty_decay_ms:1000,narenas:2,background_thread:true'

Alpine

FROM ruby:3.2.3-alpine3.19
RUN apk add --update jemalloc
ENV LD_PRELOAD=/usr/lib/libjemalloc.so.2
ENV MALLOC_CONF='dirty_decay_ms:1000,narenas:2,background_thread:true'

Heroku

  1. add the apt buildpack, which is maintained by Heroku.
    heroku buildpacks:add --index 1 heroku-community/apt
  2. create an Aptfile with this in it, and deploy it.
    libjemalloc2
    
  3. Set these ENV vars:
    heroku config:set \
      LD_PRELOAD="/app/.apt/usr/lib/x86_64-linux-gnu/libjemalloc.so.2" \
      MALLOC_CONF="dirty_decay_ms:1000,narenas:2,background_thread:true"

There is also the third-party heroku-buildpack-jemalloc, which will download, build, and install an arbitrary jemalloc version, which is useful if you need a version not distributed by Ubuntu for some reason, such as before the discovery of the improved MALLOC_CONF, necessitating the installation of version 3.6

MALLOC_CONF options

  • dirty_decay_ms:1000,narenas:2,background_thread:true comes from dockerfile-rails. code discussion
  • This is the original "tradeoff between memory and performance". it is likely identical to the above config on recent Jemalloc and Ruby versions, because the extra values are the defaults dirty_decay_ms:1000,muzzy_decay_ms:0,narenas:2,background_thread:true,thp:never
  • This is "more heavily memory-optimized, like jemalloc 3.6" (the version used by Fullstaq Ruby) dirty_decay_ms:0,muzzy_decay_ms:0,narenas:2,background_thread:true,thp:never
  • This is used by gitlab 1 2 narenas:2

Check if jemalloc is working:

MALLOC_CONF="stats_print:true" ruby -e "exit" # will produce no output if jemalloc not being used, a wall of text if it is
MALLOC_CONF="$MALLOC_CONF,stats_print:true" ruby -e "exit" # if a config is set, you can include it like this

Links!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment