Jemalloc on Percona ServerThis post discusses enabling and disabling jemalloc on Percona Server for MySQL.

The benefits of jemalloc versus glibc for use with MySQL have been widely discussed. With jemalloc (along with Transparent Huge Pages disabled) you have less memory fragmentation, and thus more efficient resource management of the available server memory.

For standard installations of Percona Server 5.6+ (releases starting with 5.6.19-67.0), the only thing needed to use jemalloc as the memory library for mysqld is for it to be installed on the server.

Enabling Jemalloc on Percona Server

First thing first: install Jemalloc.

The library is available on the Percona repository, which is available for both apt and yum package management:

Once you have the repo, just run the install command (according to your OS) to install it:

Now that you have the jemalloc package installed, all it takes to start using it is…..

  • Restart the server.

That’s it! No modifications needed on the my.cnf file or anywhere else. Plain and simple!

Disabling Jemalloc on Percona Server

If for any reason you need to disable jemalloc and go back to the default library, you have two options: remove the jemalloc package (not too practical), or add the following line to the [mysqld_safe] section of the my.cnf file:

In other words, an empty path. That will do the trick. Note that commenting or removing the “malloc-lib” parameter on the cnf file won’t work.

How to Know if Jemalloc is Being Used?

There are couple of ways you can verify this, but the less invasive way is by using the pt-mysql-summary (version 2.2.20 and higher) tool from the Percona Toolkit:

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Seveny Media

Running Persona Server 5.7 here and wasn’t able to find a package called ‘jemalloc’

# apt-cache search jemalloc
libjemalloc-dev – development files and documentation for jemalloc
libjemalloc1 – general-purpose scalable concurrent malloc(3) implementation
libjemalloc1-dbg – debug symbols for jemalloc

Installed ‘libjemalloc1’ and it works perfectly:

# pt-mysql-summary | grep -A5 -i “memory management”
# Memory management library ##################################
jemalloc enabled in MySQL config for process with ID 24470
Using jemalloc from /usr/lib/x86_64-linux-gnu/libjemalloc.so.1
# The End ####################################################

Olivier Doucet

Please note that this requires Percona Toolkit 3.0+ 😉

Lee

I am using mysql-5.7.22 and 23, these two versions, using jemalloc-5.1, combined with MTS, there will be a problem with the memory leak, after the jemalloc is disabled, the memory usage is normal.

GravatarRei

use the stable version like jemmalloc 3.6.0-11 (from apt-get install libjemmaloc1)

geyilat696

Is it tutorial actual for MySQL 8.0 ?
Looks like not for me.

root@server:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.3 LTS
Release: 18.04

root@server:~# mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33
Server version: 8.0.18-9 Percona Server (GPL), Release ‘9’, Revision ’53e606f’

root@server:~# apt-cache search jemalloc
libjemalloc-dev – development files and documentation for jemalloc
libjemalloc1 – general-purpose scalable concurrent malloc(3) implementation

root@server:~# dpkg -L libjemalloc1
/.
/usr
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libjemalloc.so.1
/usr/share
/usr/share/doc
/usr/share/doc/libjemalloc1
/usr/share/doc/libjemalloc1/README
/usr/share/doc/libjemalloc1/changelog.Debian.gz
/usr/share/doc/libjemalloc1/copyright

root@server:~# cat /etc/sysconfig/mysql
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1

root@server:~# sudo /etc/init.d/mysql start
[ ok ] Starting mysql (via systemctl): mysql.service.

root@server:~# pt-mysql-summary –ask-pass | grep -A5 -i “memory management”
# Memory management library ##################################
jemalloc is not enabled in mysql config for process with id 1079
# The End ####################################################