Optimizing Nginx and MySQL Memory Management with jemalloc

jemallocnginx memory optimizationmysql memory managementmemory allocatorconcurrent memory allocation
Published·Modified·

jemalloc emphasizes fragmentation avoidance and scalable concurrent support. First introduced as the FreeBSD libc allocator in 2005, it has since been adopted by many applications relying on its predictable behavior. jemalloc is well-suited for memory allocation management in multi-threaded environments. Evaluation results from various sources show that jemalloc performs on par with Google's tcmalloc, both representing the highest level in the field of memory management.

Performance Comparison

Installing jemalloc

You can obtain the latest version of jemalloc from GitHub: https://github.com/jemalloc/jemalloc/releases. The installation method is as follows (for reference only):

# Install dependencies
yum -y install gcc gcc-c++

# Download jemalloc
wget http://soft.xiaoz.org/linux/jemalloc-5.2.0.tgz
tar -zxvf jemalloc-5.2.0.tgz
cd jemalloc-5.2.0

# Install jemalloc
./configure
make && make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig

Optimizing MySQL with jemalloc

For MySQL/MariaDB 5.5 compilation, add the following parameters during the CMake pre-configuration step:

-DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" -DWITH_SAFEMALLOC=OFF

Optimizing Nginx with jemalloc

When compiling Nginx, simply add the --with-ld-opt=-ljemalloc parameter.

Verification

To check if jemalloc is active, run the following command:

lsof -n | grep jemalloc

The screenshot below shows the result:

Verification Screenshot

This article references content from: jemalloc optimization for MySQL and Nginx memory management