docker: switch Ruby memory allocator to Jemalloc.

Switch the Ruby memory allocator from Glibc malloc to Jemalloc. Jemalloc
supposedly uses less memory than Glibc malloc because it's better at
handling memory fragmentation. It also has detailed internal statistics
to help monitor allocator behavior.

We use the LD_PRELOAD method of loading Jemalloc instead of building it
into Ruby so that we can switch allocators at runtime.
This commit is contained in:
evazion
2021-12-18 22:49:21 -06:00
parent 0ba6dc9ee5
commit 4d9028f1fd
2 changed files with 3 additions and 5 deletions

View File

@@ -3,10 +3,6 @@ FROM ubuntu:21.04 AS base
WORKDIR /danbooru WORKDIR /danbooru
ENV PATH="/root/.asdf/bin:/root/.asdf/shims:$PATH:/usr/lib/postgresql/14/bin" ENV PATH="/root/.asdf/bin:/root/.asdf/shims:$PATH:/usr/lib/postgresql/14/bin"
# Reduces memory usage at the cost of higher thread contention.
# https://bugs.ruby-lang.org/issues/14718
ENV MALLOC_ARENA_MAX=2
COPY config/docker/build-base-image.sh . COPY config/docker/build-base-image.sh .
RUN \ RUN \
@@ -64,6 +60,8 @@ RUN \
FROM base as production FROM base as production
ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2
COPY --from=development /root /root COPY --from=development /root /root
COPY --from=development /danbooru /danbooru COPY --from=development /danbooru /danbooru

View File

@@ -25,7 +25,7 @@ DANBOORU_RUNTIME_DEPS="
ca-certificates mkvtoolnix rclone libpq5 openssl libgmpxx4ldbl ca-certificates mkvtoolnix rclone libpq5 openssl libgmpxx4ldbl
zlib1g libfftw3-3 libwebp6 libwebpmux3 libwebpdemux2 liborc-0.4.0 liblcms2-2 zlib1g libfftw3-3 libwebp6 libwebpmux3 libwebpdemux2 liborc-0.4.0 liblcms2-2
libpng16-16 libexpat1 libglib2.0 libgif7 libexif12 libheif1 libvpx6 libpng16-16 libexpat1 libglib2.0 libgif7 libexif12 libheif1 libvpx6
libseccomp2 libseccomp-dev libseccomp2 libseccomp-dev libjemalloc2
" "
COMMON_RUNTIME_DEPS=" COMMON_RUNTIME_DEPS="
$DANBOORU_RUNTIME_DEPS $EXIFTOOL_RUNTIME_DEPS tini busybox less ncdu $DANBOORU_RUNTIME_DEPS $EXIFTOOL_RUNTIME_DEPS tini busybox less ncdu