The benefits of jemalloc versus glibc memory allocator for use with MySQL have been widely discussed. With jemalloc (along with Transparent Huge Pages disabled) there is less memory fragmentation, and thus more efficient resource management of the server memory. For MySQL 5.6, installing jemalloc is enough to enable it when starting the MySQL process. However, for MySQL 5.7 and 8.0.X series, you will need to take a few extra steps.
Enabling jemalloc on Percona Server for MySQL
Installing the jemalloc package is simple for Percona. This is because the library is available on the Percona repository, which is available for both apt
and yum
package management:
- Installing Percona Server for MySQL on Debian and Ubuntu
- Installing Percona Server for MySQL on Red Hat Enterprise Linux and CentOS
Once you have the repo, just run the install command (according to your OS) to install it:
Now that the package is installed, we need to make MySQL working with it. And now, it depends on how you are starting MySQL:
1 |
yum install jemalloc / apt-get install libjemalloc1 |
Using systemd Services (systemctl
Command)
1 2 |
# cat /etc/sysconfig/mysql LD_PRELOAD=/usr/lib64/libjemalloc.so.1 |
2-) Start MySQL
1 |
systemctl start mysql |
Starting Manually
If you are starting it manually, in the command line, you need to specify the library in the LD_PRELOAD. So the command to initialize will be similar to this one:
1 |
LD_PRELOAD=/usr/lib64/libjemalloc.so.1 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid --user mysql |
Check if MySQL is Using jemalloc
pt-mysql-summary
which is part of the Percona Toolkit:
1 2 3 4 5 |
[root@localhost ~]# pt-mysql-summary | grep -A5 -i "memory management" # Memory management library ################################## jemalloc enabled in mysql config for process with id 14490 Using jemalloc from /usr/lib64/libjemalloc.so.1 # The End #################################################### |
And you can also do it manually:
1 2 |
pid=$(pidof mysqld) cat /proc/${pid}/environ |
Or:
1 |
lsof -Pn -p $(pidof mysqld) | grep jemalloc |
Conclusion
This blog post was written to update the guidelines of jemalloc
. Important to mention that using the recommendations above regarding systemd
, the changes will persist across MySQL upgrades.
Finally, jemalloc
still is a good memory allocator and I consider it part of the best practices that should be implemented before the deployment in production.
Useful Resources
Finally, you can reach us through the social networks, our forum, or access our material using the links presented below:
It should be noted, that the jemalloc version packaged with Percona is very old. It’s a 3.6.0 from April 15th 2015. The current version is 5.3.0 from May 06th 2022 provided here: https://github.com/jemalloc/jemalloc/releases/tag/5.3.0)
And this is well known. See issue “Bump jemalloc RPM/DEB packages to 5.2” (https://jira.percona.com/browse/ENG-1073)
Sadly it’s a “won’t fix“.
Maybe the blog post can be updated?
Moreover, the packaged version in the ps-80-release-x86_64 repository has the epoche tag set to 1. So even if you install jemalloc from the EPEL repository it tries to update it every time, because if the epoch is set in a package, then the package with the highest epoch value is installed as the current package. Version and release are ignored. If not set, the epoch defaults to 0.
Hi Daniel,
Thanks for the observation! I spoke with our engineering team and got some feedback from them. With regards to PS 5.7. jemalloc was required because of TokuDB. Given that MySQL 5.7 is reaching its end of life, the engineering team decided not to update the jemalloc package. In this case, you will have to build or find a more recent package.
For PS 8, now that TokuDB is deprecated, engineering opened an internal discussion to remove the jemalloc from the package for future releases. There is no deadline defined yet, but your inputs were very important for them!
Let me know if you have any further questions, and thanks for taking the time to look into this!