From 0988e2d9d653899207caf853f96f7d2903a137bf Mon Sep 17 00:00:00 2001 From: Abhinav Dhiman <8640877+ahnv@users.noreply.github.com> Date: Sun, 24 May 2026 17:32:50 +0530 Subject: [PATCH] perf: use jemalloc as the default allocator (#1910) * feat(docker): add jemalloc to reduce memory fragmentation Install libjemalloc2 in the base image and preload it via LD_PRELOAD in docker-entrypoint when available. Reduces RSS growth from glibc's default allocator fragmentation under Rails workloads. * feat(docker): add DISABLE_JEMALLOC env var + preserve existing LD_PRELOAD * feat(docker): add jemalloc status logging to entrypoint * refactor(docker): simplify jemalloc logging to warn-only when disabled/missing --- Dockerfile | 4 ++-- bin/docker-entrypoint | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2cb2c83e7..c165b18f7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ WORKDIR /rails # Install base packages RUN apt-get update -qq \ - && apt-get install --no-install-recommends -y curl libvips postgresql-client libyaml-0-2 procps \ + && apt-get install --no-install-recommends -y curl libvips postgresql-client libyaml-0-2 procps libjemalloc2 \ && rm -rf /var/lib/apt/lists /var/cache/apt/archives # Set production environment @@ -19,7 +19,7 @@ ENV RAILS_ENV="production" \ BUNDLE_PATH="/usr/local/bundle" \ BUNDLE_WITHOUT="development" \ BUILD_COMMIT_SHA=${BUILD_COMMIT_SHA} - + # Throw-away build stage to reduce size of final image FROM base AS build diff --git a/bin/docker-entrypoint b/bin/docker-entrypoint index 67ef49314..672521a98 100755 --- a/bin/docker-entrypoint +++ b/bin/docker-entrypoint @@ -1,5 +1,15 @@ #!/bin/bash -e +# Use jemalloc to reduce memory fragmentation if available +JEMALLOC="/usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2" +if [ -f "$JEMALLOC" ] && [ -z "${DISABLE_JEMALLOC}" ]; then + export LD_PRELOAD="${LD_PRELOAD:+$LD_PRELOAD:}$JEMALLOC" +else + [ -n "${DISABLE_JEMALLOC}" ] \ + && echo "WARNING: jemalloc disabled via DISABLE_JEMALLOC" \ + || echo "WARNING: jemalloc not found at $JEMALLOC, skipping" +fi + # If running the rails server then create or migrate existing database if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then ./bin/rails db:prepare