Skip to content

Ruby with jemalloc #182

Closed
Closed

Description

@Vad1mo

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.

Activity

  1. jonmoter commented on Feb 20, 2018

    @jonmoter

    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.

  2. Vad1mo commented on Feb 20, 2018

    @Vad1mo
    Author

    @jonmoter could you open PR, in [WIP] status I will also contribute to this PR and add my stuff

  3. libo commented on Feb 20, 2018

    @libo

    @Vad1mo @jonmoter something like this? #190

  4. added
    RequestRequest for image modification or feature
    on Apr 25, 2018
  5. jorihardman commented on Nov 14, 2018

    @jorihardman

    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.

  6. shleeable commented on Nov 27, 2018

    @shleeable
    Contributor

    @jorihardman amazing work.. can you open the issue register + any plans to support Alpine?

  7. jorihardman commented on Nov 27, 2018

    @jorihardman

    @ashleyhull-versent thanks for the alpine PR. Images are being built as a type this.

  8. yosifkit commented on Jan 21, 2019

    @yosifkit
    Member

    #198 (comment)

    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. 🎉 🌮

  9. pinned this issue on Jan 21, 2019
  10. ftuyama commented on Jan 23, 2019

    @ftuyama

    @yosifkit I tried using LD_PRELOAD, but I have no clue why jemalloc isn't being loaded:

    FROM ruby:2.4.2
    
    RUN apt-get update && apt-get install -y libjemalloc-dev libjemalloc1
    ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1
    
    ...

    Then, inside the container:

    > echo $LD_PRELOAD
    /usr/lib/x86_64-linux-gnu/libjemalloc.so.1
    > ruby -r rbconfig -e "puts RbConfig::CONFIG['LIBS']"
    -lpthread -ldl -lcrypt -lm

    Any idea why jemalloc isn't listed? 😿

    Edit: ruby:2.6 won't work either.

  11. tjwallace commented on Jan 23, 2019

    @tjwallace

    @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)

  12. cesar-faria commented on Jan 23, 2019

    @cesar-faria

    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']. 😢

  13. ftuyama commented on Jan 23, 2019

    @ftuyama

    Using a docker image where ruby is compiled using jemalloc, jemalloc is actually in MAINLIBS: https://hub.docker.com/r/swipesense/ruby-jemalloc

    > ruby -r rbconfig -e "puts RbConfig::CONFIG['MAINLIBS']"
    -lz -lpthread -lrt -lrt -ljemalloc -ldl -lcrypt -lm 

    But I couldn't load it with LD_PRELOAD.

  14. tianon commented on Jan 23, 2019

    @tianon
    Member
  15. butsjoh commented on Jan 25, 2019

    @butsjoh

    Normally when you do the following (when you see output) ruby should be using jemalloc.

    MALLOC_CONF=stats_print:true ruby -e "exit"
    
  16. 17 remaining items

  17. fabioperrella commented on Sep 3, 2021

    @fabioperrella

    Is there a recommendation of which libjemalloc version to use?

    I see in the messages above that libjemalloc1 is being used with Ruby2.6 and libjemalloc2 with Ruby3.

    Can I use, for example, libjemalloc2with ruby:2.6-buster?

  18. jasoncodes commented on Sep 3, 2021

    @jasoncodes

    We’re using libjemalloc2 on ruby:2.7.4-slim-bullseye via ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 without issue.

  19. YurySolovyov commented on Oct 8, 2021

    @YurySolovyov

    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.

  20. skhanna1011 commented on Jan 18, 2022

    @skhanna1011

    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.2

    Screenshot 2022-01-19 at 2 01 16 AM

  21. yosifkit commented on Jan 22, 2022

    @yosifkit
    Member

    @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.

  22. tianon commented on Jun 8, 2022

    @tianon
    Member

    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).

    I believe https://bugs.ruby-lang.org/issues/14718 is probably the best (upstream) place to watch for more on this topic.

  23. Earlopain commented on Dec 1, 2022

    @Earlopain
    Contributor

    Jemalloc is back in the alpine repository for 3.17. If you use that variant there is no need for manual builds anymore.

    RUN apk --no-cache add jemalloc
    ENV LD_PRELOAD=/usr/lib/libjemalloc.so.2

    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

  24. josacar commented on Jan 1, 2023

    @josacar

    If you don't to use LD_PRELOAD as it affects to every binary you can patch ruby binary like this:

    FROM ruby:2.7-slim-bullseye
    RUN apt-get update  && \
      apt-get upgrade -y && \
      apt-get install libjemalloc2 patchelf && \
      patchelf --add-needed libjemalloc.so.2 /usr/local/bin/ruby && \
      apt-get purge patchelf -y
    
  25. neongrau commented on Apr 5, 2023

    @neongrau

    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
    
  26. nuzayets commented on May 31, 2023

    @nuzayets

    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:

    $ ldconfig -v 2> /dev/null | grep linux-gnu
    /lib/aarch64-linux-gnu:
    /usr/lib/aarch64-linux-gnu:
    $ ldconfig -v 2> /dev/null | grep libjemalloc
            libjemalloc.so.2 -> libjemalloc.so.2

    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.

    $ export | grep LD_PRELOAD
    declare -x LD_PRELOAD="libjemalloc.so.2"
    $ MALLOC_CONF=stats_print:true ruby -e "exit"
    ___ Begin jemalloc statistics ___
    ....
  27. jjb commented on Dec 12, 2023

    @jjb

    Here is a collection of info on ruby+Jemalloc5 https://gist.github.com/jjb/9ff0d3f622c8bbe904fe7a82e35152fc

  28. pascalbetz commented on May 22, 2024

    @pascalbetz

    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)

  29. ibrahima commented on Apr 7, 2025

    @ibrahima

    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

  30. tianon commented on Aug 22, 2025

    @tianon
    Member

    https://bugs.ruby-lang.org/issues/14718#change-114362

    • Status changed from Open to Rejected

    jemalloc development has been discontinued.

    https://jasone.github.io/2025/06/12/jemalloc-postmortem/

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    RequestRequest for image modification or feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions