You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to create a ruby version compiled with jemalloc. Is this something the community needs? Is it encouraged to make an official contribution. If, so I can try to create an official contribution.
Reacted by Marco Costa, Bryan Rite, Jon Moter, Pierre Schambacher, Anatoly Mikhaylov, Javier Palomo, Nicky Valentino, Mark Oleson, Alfonso, Lithium and 97 more
Reacted by Anatoly Mikhaylov, Javier Palomo, Nicolas Rodriguez, Roberto Schneiders, Ronaldo Raivil, Kasper Grubbe, Nathan Broadbent, twirl, Van Driessche Vincent, Alexis LE TEXIER and 10 more
We'd like to do the same thing, and have started down the path of building our own ruby docker image with jemalloc support. That involves a whole bunch of copy & pasting of Dockerfiles from this repo.
So it would be great if this option could be included as an option for the images published on docker hub. And I think a lot of other ruby users would find this valuable.
Reacted by Kasper Grubbe, Roey D. Chasman, L, Nikita Shatov, Stijn V and Mikael Henriksson
We've been using this in production for a couple weeks now and have seen great results. I will maintain that docker hub and repo until this repo implements this request.
Reacted by Tianon Gravi, foxtacles, Nathan Broadbent, Iuri G., Felipe Tuyama, Gecko Fu, Youssef Idmoussi, Pere Joan Martorell and Dat Le-Tien
Reacted by Tianon Gravi, Shlee, Vadim Bauer, foxtacles, Nathan Broadbent, Iuri G., Felipe Tuyama, Alex Simenduev, Gecko Fu, Youssef Idmoussi and 3 more
We're still hesitant to add this to the image without a recommendation by Ruby upstream. In the meantime, you can trivially add support in your own Dockerfile without recompiling ruby through the magic of LD_PRELOAD:
FROM ruby:2.6
RUN apt-get update && apt-get install libjemalloc1 && rm -rf /var/lib/apt/lists/*
ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1
# gleaned from a few sources# https://github.com/jemalloc/jemalloc/wiki/Getting-Started# https://brandonhilkert.com/blog/reducing-sidekiq-memory-usage-with-jemalloc/
And done, ruby now uses jemalloc. 🎉 🌮
Reacted by Jarek Radosz, Gecko Fu, Leandro Pereira, Gustavo Chapim, florianhidayat, VitaliyAdamkov, Victor Lellis, chad, L, nnlinh1985 and 35 more
Reacted by Paul Alden, Frederik-Baetens, Pedro Carriço, WillisChou, Carlos Quintero, Emil Ong, Eurico Vidal, Oliver Azevedo Barnes, Mac and Nathan Broadbent
@ftuyama in Ruby 2.6 I had to look for jemalloc in RbConfig::CONFIG['MAINLIBS'] (this is on my laptop, not in docker - we use the alpine image and there is an issue with jemalloc/musl right now)
Reacted by Julian Rubisch, Felipe Tuyama, Alex Emelyanov and Rabin Shrestha
Normally when you do the following (when you see output) ruby should be using jemalloc.
MALLOC_CONF=stats_print:true ruby -e "exit"
Reacted by Jarek Radosz, Lior Goikhburg, Felipe Tuyama, Leandro Pereira, Albert Llop, Gabriela Moreira, Jonathan Hurter, 97jaz, florianhidayat, Oskar Pearson and 53 more
Reacted by Ahmed Yakout, Szikszai Gusztáv, Kevin Ke, Pedro Carriço, Mehdi Zonjy, Ulysse Buonomo, leonardk1, Eurico Vidal, Dorian Marié, John Bachir and 5 more
3.0.2 on alpine 3.14 seems to work too (requires "manual" jemalloc build tho) Dockerfile:
FROM ruby:3.0.2-alpine3.14 AS builder
RUN apk add build-base
RUN wget -O - https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2 | tar -xj && \
cd jemalloc-5.2.1 && \
./configure && \
make && \
make install
FROM ruby:3.0.2-alpine3.14
COPY --from=builder /usr/local/lib/libjemalloc.so.2 /usr/local/lib/
ENV LD_PRELOAD=/usr/local/lib/libjemalloc.so.2
RUN MALLOC_CONF=stats_print:true ruby -e "exit"# ... your stuffs
^ this one is multistage, but does not have to be.
@skhanna1011, Adding ENV LD_PRELOAD to get jemalloc is meant for use when running a ruby based application. As you've encountered, it seems like setting it while compiling gem native extensions (via gem install) can cause build issues. I don't see anything we could change in the image that would help with this.
Without an explicit upstream recommendation for using jemalloc by default, I hesitate to make an actual image change here (especially given that there are really functional workarounds that allow users to enable jemalloc in a dependent image without too much trouble).
In case anyone else wants to have a Dockerfile that runs on different architectures. Co-workers run it on x86_64 while i run it on aarch64. This now works on both without the need to adjust directory or throw around docker build arguments.
RUN ln -s /usr/lib/*-linux-gnu/libjemalloc.so.2 /usr/lib/libjemalloc.so.2
ENV LD_PRELOAD=/usr/lib/libjemalloc.so.2
Reacted by Faaz, Rafał W, jonathan schatz, Gar, Arturs Mekss, Frederik Spang, Alexander Kuznetsov, Aubermean, Abe Wheeler, Guk0 and 3 more
In case anyone else wants to have a Dockerfile that runs on different architectures. Co-workers run it on x86_64 while i run it on aarch64. This now works on both without the need to adjust directory or throw around docker build arguments.
RUN ln -s /usr/lib/*-linux-gnu/libjemalloc.so.2 /usr/lib/libjemalloc.so.2
ENV LD_PRELOAD=/usr/lib/libjemalloc.so.2
At least on Debian and Debian derivatives an exact path doesn't seem to be required. If you apt install libjemalloc2, the /usr/lib/*-linux-gnu directory is already configured in the default LD path:
Therefore the following should work across architectures:
ENV LD_PRELOAD=libjemalloc.so.2
And indeed that is the behaviour we see in production across aarch64 and x86_64, i.e. you can refer to the library in LD_PRELOAD without fully qualifying it.
Using LD_PRELOAD with Ruby > 3.0 (tried 3.1, 3.2 and 3.3) and mini_racer causes segfaults, statically linked seems to be okay though.
See rubyjs/mini_racer#292 (comment)
Activity
jonmoter commentedon Feb 20, 2018
We'd like to do the same thing, and have started down the path of building our own ruby docker image with jemalloc support. That involves a whole bunch of copy & pasting of
Dockerfilesfrom this repo.So it would be great if this option could be included as an option for the images published on docker hub. And I think a lot of other ruby users would find this valuable.
Vad1mo commentedon Feb 20, 2018
@jonmoter could you open PR, in [WIP] status I will also contribute to this PR and add my stuff
libo commentedon Feb 20, 2018
@Vad1mo @jonmoter something like this? #190
jorihardman commentedon Nov 14, 2018
If anyone is looking to run this now, I've forked this repository, merged #192, and added a public/automated docker hub that can be found here: https://hub.docker.com/r/swipesense/ruby-jemalloc
We've been using this in production for a couple weeks now and have seen great results. I will maintain that docker hub and repo until this repo implements this request.
shleeable commentedon Nov 27, 2018
@jorihardman amazing work.. can you open the issue register + any plans to support Alpine?
jorihardman commentedon Nov 27, 2018
@ashleyhull-versent thanks for the alpine PR. Images are being built as a type this.
yosifkit commentedon Jan 21, 2019
We're still hesitant to add this to the image without a recommendation by Ruby upstream. In the meantime, you can trivially add support in your own Dockerfile without recompiling ruby through the magic of
LD_PRELOAD:And done, ruby now uses jemalloc. 🎉 🌮
ftuyama commentedon Jan 23, 2019
@yosifkit I tried using
LD_PRELOAD, but I have no clue why jemalloc isn't being loaded:Then, inside the container:
Any idea why jemalloc isn't listed? 😿
Edit: ruby:2.6 won't work either.
tjwallace commentedon Jan 23, 2019
@ftuyama in Ruby 2.6 I had to look for
jemallocinRbConfig::CONFIG['MAINLIBS'](this is on my laptop, not in docker - we use the alpine image and there is an issue with jemalloc/musl right now)cesar-faria commentedon Jan 23, 2019
I am having trouble with jemalloc not being loaded as well.
@tjwallace I am using Ruby 2.6 and I got nothing in
RbConfig::CONFIG['MAINLIBS']. 😢ftuyama commentedon Jan 23, 2019
Using a docker image where ruby is compiled using jemalloc, jemalloc is actually in
MAINLIBS: https://hub.docker.com/r/swipesense/ruby-jemallocBut I couldn't load it with
LD_PRELOAD.tianon commentedon Jan 23, 2019
butsjoh commentedon Jan 25, 2019
Normally when you do the following (when you see output) ruby should be using jemalloc.
17 remaining items
fabioperrella commentedon Sep 3, 2021
Is there a recommendation of which
libjemallocversion to use?I see in the messages above that
libjemalloc1is being used with Ruby2.6 andlibjemalloc2with Ruby3.Can I use, for example,
libjemalloc2withruby:2.6-buster?jasoncodes commentedon Sep 3, 2021
We’re using
libjemalloc2onruby:2.7.4-slim-bullseyeviaENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2without issue.YurySolovyov commentedon Oct 8, 2021
3.0.2 on alpine 3.14 seems to work too (requires "manual" jemalloc build tho)
Dockerfile:^ this one is multistage, but does not have to be.
skhanna1011 commentedon Jan 18, 2022
RUN gem install racc -v '1.5.2'gives an error with libjemalloc2 (works fine without it), while building linux/x86_64 image on M1 Mac.FROM ruby:2.7.1-slim RUN apt-get update -qq apt-get install -y --no-install-recommends build-essential libjemalloc2 ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2yosifkit commentedon Jan 22, 2022
@skhanna1011, Adding
ENV LD_PRELOADto get jemalloc is meant for use when running a ruby based application. As you've encountered, it seems like setting it while compiling gem native extensions (viagem install) can cause build issues. I don't see anything we could change in the image that would help with this.tianon commentedon Jun 8, 2022
Without an explicit upstream recommendation for using
jemallocby default, I hesitate to make an actual image change here (especially given that there are really functional workarounds that allow users to enablejemallocin a dependent image without too much trouble).I believe https://bugs.ruby-lang.org/issues/14718 is probably the best (upstream) place to watch for more on this topic.
Earlopain commentedon Dec 1, 2022
Jemalloc is back in the alpine repository for 3.17. If you use that variant there is no need for manual builds anymore.
Or use jemalloc.sh which automatically sets up LD_PRELOAD for a single process:
MALLOC_CONF=stats_print:true jemalloc.sh ruby -e "exit"https://pkgs.alpinelinux.org/package/v3.17/main/x86_64/jemalloc
josacar commentedon Jan 1, 2023
If you don't to use
LD_PRELOADas it affects to every binary you can patchrubybinary like this:neongrau commentedon Apr 5, 2023
In case anyone else wants to have a Dockerfile that runs on different architectures. Co-workers run it on x86_64 while i run it on aarch64. This now works on both without the need to adjust directory or throw around docker build arguments.
nuzayets commentedon May 31, 2023
At least on Debian and Debian derivatives an exact path doesn't seem to be required. If you
apt install libjemalloc2, the/usr/lib/*-linux-gnudirectory is already configured in the default LD path:Therefore the following should work across architectures:
ENV LD_PRELOAD=libjemalloc.so.2And indeed that is the behaviour we see in production across aarch64 and x86_64, i.e. you can refer to the library in
LD_PRELOADwithout fully qualifying it.jjb commentedon Dec 12, 2023
Here is a collection of info on ruby+Jemalloc5 https://gist.github.com/jjb/9ff0d3f622c8bbe904fe7a82e35152fc
pascalbetz commentedon May 22, 2024
Using LD_PRELOAD with Ruby > 3.0 (tried 3.1, 3.2 and 3.3) and mini_racer causes segfaults, statically linked seems to be okay though.
See rubyjs/mini_racer#292 (comment)
ibrahima commentedon Apr 7, 2025
We ran into an issue using jemalloc 3.6.0 via LD_PRELOAD on Ubuntu 24.04 (due to glibc 2.39, I believe). This patch fixes it, in case it's useful for others: https://gist.github.com/ibrahima/fcc65e084e79b6519ac6fb0da5005378
tianon commentedon Aug 22, 2025
https://bugs.ruby-lang.org/issues/14718#change-114362