Memory allocation efficiency is critical to maximizing the performance of MySQL databases, especially in high-traffic, write-intensive environments like Percona XtraDB Cluster. The default system memory allocator (such as glibc’s malloc) can lead to significant memory fragmentation and inefficiencies under heavy workloads, ultimately limiting database throughput. Deploying an alternative memory allocator—such as jemalloc—can provide substantial improvements in resource management and overall cluster performance.
Why Use jemalloc for MySQL?
jemalloc is designed to reduce memory fragmentation and provide faster memory allocation and deallocation compared to the standard glibc allocator. MySQL, particularly under workloads with frequent concurrent queries and complex transactions, can greatly benefit from jemalloc’s optimized algorithms. Industry benchmarks and internal testing (see Percona’s blog) consistently demonstrate improved performance, with some environments observing up to double the query throughput after switching to jemalloc.
Installation and Configuration Steps
1. Install jemalloc
To install jemalloc on a CentOS or RHEL-based system, execute the following command as the root user:
yum install jemalloc
This will place the jemalloc shared library (usually /usr/lib64/libjemalloc.so.1) on your system.
2. Configure MySQL to Use jemalloc
To instruct MySQL to use jemalloc, add the following lines to your my.cnf configuration file under the [mysqld_safe] section:
[mysqld_safe] malloc-lib=/usr/lib64/libjemalloc.so.1
Tip: Always verify the correct path for
libjemalloc.so.1on your system. You can use thefind /usr -name 'libjemalloc.so*'command to locate the library if it is installed in a non-standard directory.
3. Restart MySQL Service
For the changes to take effect, restart the MySQL service:
service mysql restart
After restarting, MySQL will utilize jemalloc for all memory allocation operations.
Right after applying jemalloc you may expect to see reduced memory fragmentation and improved query throughput, particularly under high concurrency. In our testing, the Percona Cluster processed nearly twice as many SQL queries on the same hardware after switching from glibc’s default allocator to jemalloc.
The graph above illustrates the significant increase in queries handled right after enabling jemalloc.