From 4d9028f1fd3d235253af3846129616dc4bcce443 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 18 Dec 2021 22:49:21 -0600 Subject: [PATCH] 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. --- config/docker/Dockerfile.danbooru | 6 ++---- config/docker/build-base-image.sh | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/config/docker/Dockerfile.danbooru b/config/docker/Dockerfile.danbooru index e65e0e8ea..746eb4d4e 100644 --- a/config/docker/Dockerfile.danbooru +++ b/config/docker/Dockerfile.danbooru @@ -3,10 +3,6 @@ FROM ubuntu:21.04 AS base WORKDIR /danbooru 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 . RUN \ @@ -64,6 +60,8 @@ RUN \ FROM base as production +ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 + COPY --from=development /root /root COPY --from=development /danbooru /danbooru diff --git a/config/docker/build-base-image.sh b/config/docker/build-base-image.sh index 0e3a60303..e3dba7600 100755 --- a/config/docker/build-base-image.sh +++ b/config/docker/build-base-image.sh @@ -25,7 +25,7 @@ DANBOORU_RUNTIME_DEPS=" ca-certificates mkvtoolnix rclone libpq5 openssl libgmpxx4ldbl zlib1g libfftw3-3 libwebp6 libwebpmux3 libwebpdemux2 liborc-0.4.0 liblcms2-2 libpng16-16 libexpat1 libglib2.0 libgif7 libexif12 libheif1 libvpx6 - libseccomp2 libseccomp-dev + libseccomp2 libseccomp-dev libjemalloc2 " COMMON_RUNTIME_DEPS=" $DANBOORU_RUNTIME_DEPS $EXIFTOOL_RUNTIME_DEPS tini busybox less ncdu