Skip to content

Instantly share code, notes, and snippets.

@toshimaru
Last active February 9, 2024 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save toshimaru/b8e528c4be807612185277cc9da52b5a to your computer and use it in GitHub Desktop.
Save toshimaru/b8e528c4be807612185277cc9da52b5a to your computer and use it in GitHub Desktop.
Enable jemalloc for alpine.
# Check server process is using jemalloc
# ref. [Japanese] https://tech.studyplus.co.jp/entry/2019/09/09/094140
class AppController < ActionController::Base
def show
r = `strings /proc/#{Process.pid}/maps | grep jemalloc`
render plain: "<h1>#{ENV['LD_PRELOAD'].presence || 'empty'}</h1><pre>#{r}</pre>"
end
end

Dockerfile

FROM ruby:2.7-alpine 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:2.7-alpine

COPY --from=builder /usr/local/lib/libjemalloc.so.2 /usr/local/lib/
ENV LD_PRELOAD=/usr/local/lib/libjemalloc.so.2

Reference

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