Optimizing Nginx and MySQL Memory Management with jemalloc

Publish: 2019-04-06 | Modify: 2019-04-06

jemalloc emphasizes fragment avoidance and scalable concurrent support. jemalloc was first used as the FreeBSD libc allocator in 2005 and has since been incorporated into many applications that rely on its predictable behavior. jemalloc is suitable for memory allocation management in multi-threaded environments, and it is considered one of the top memory managers in the field, along with Google tcmalloc, as evidenced by various performance evaluations. The installation process for jemalloc 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

To optimize MySQL with jemalloc, add the following parameters when compiling with cmake:

-DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" -DWITH_SAFEMALLOC=OFF

To optimize Nginx with jemalloc, simply add the --with-ld-opt=-ljemalloc parameter when compiling Nginx.

Finally, you can use the command lsof -n | grep jemalloc to check if jemalloc is already in effect. See the screenshot below:

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


Comments