Table of contents of the article:
Introduction
When managing high-performance databases like MySQL and MariaDB, memory allocation plays a critical role in the efficiency of read and write operations. Standard memory allocators, such as glibc's malloc, can be limited in high-load scenarios. Alternatives like Jemalloc and TCMalloc offer significant improvements in memory management, reducing fragmentation and improving overall database efficiency.
In this article, we will look at the advantages of Jemalloc and TCMalloc over standard malloc, how to integrate them into MySQL and MariaDB, and how to monitor their performance.
Why replace malloc with glibc?
The memory allocator provided by default by the GNU C library (malloc) is designed to accommodate a wide range of applications, but is not optimized for high-performance environments such as relational databases, where efficient memory management is critical. When MySQL or MariaDB operate with large amounts of data and concurrent requests, malloc can become a bottleneck, negatively impacting overall system performance.
Here are some of the main problems that can arise with the use of malloc in high-load database scenarios:
1. Fragmentation of memory
One of the main problems of malloc is memory fragmentation, which occurs when memory is allocated and freed unevenly. Over time, this can lead to an increase in unused space, reducing memory efficiency and causing excessive RAM consumption. In workload-intensive environments, such as databases, this fragmentation can result in increased operating costs and decreased system stability.
2. Limited scalability in multi-threaded systems
Modern MySQL and MariaDB installations use a highly concurrent architecture, with multiple threads accessing memory simultaneously. malloc glibc uses a global locking system that can limit the efficiency of the database if there are many simultaneous connections. This can cause a crash effect. contention, where threads compete for access to memory resources, slowing down query response time and reducing overall throughput.
3. Unpredictable performance over time
As malloc is not specifically designed for constant and intensive workloads, its efficiency may degrade over time. Due to inefficient management of allocations and deallocations, problems such as memory bloat, or the accumulation of unused memory that is not immediately returned to the operating system. This can lead to a progressive deterioration in database performance, with higher latencies in read and write operations.
4. Inefficient memory release
Another limitation of malloc is that it does not always return unused memory to the operating system in a timely manner. In highly intensive databases, this can lead to excessive accumulation of resident memory (RSS), increasing the risk of swapping and degrading overall server performance.
Jemalloc
What is Jemalloc?
Jemalloc is a memory allocator originally developed by Jason Evans for FreeBSD and is used in many high-performance applications such as Redis and Facebook.
Benefits of Jemalloc:
- Less memory fragmentation
- Better memory management in multi-threaded workloads
- Greater efficiency in allocation and deallocation
- Advanced debugging capabilities with profiling tools
Jemalloc Installation
On AlmaLinux and RHEL based distributions , you can install Jemalloc with:
sudo dnf install jemalloc -y
On Debian/Ubuntu:
sudo apt install libjemalloc-dev -y
Configuring MySQL/MariaDB with Jemalloc
To make MySQL or MariaDB use Jemalloc, you can start the server with the library preloaded. For example:
LD_PRELOAD=/usr/lib64/libjemalloc.so.2 mysqld
To make the configuration permanent, edit the systemd service file:
sudo systemctl edit mariadb
Add the following lines:
[Service]Environment="LD_PRELOAD=/usr/lib64/libjemalloc.so.2"
Then reload the service:
sudo systemctl daemon-reexec
sudo systemctl restart mariadb
TCMalloc
What is TCMalloc?
TCMalloc (Thread-Caching Malloc) is another optimized allocator, developed by Google, and is particularly useful for reducing latency in multi-threaded workloads.
Advantages of TCMalloc:
- Advanced memory caching management for each thread
- Lower latency in allocation and deallocation operations
- High performance in high load scenarios
Installing TCMalloc
To install TCMalloc on AlmaLinux/RHEL:
sudo dnf install gperftools-libs -y
On Debian/Ubuntu:
sudo apt install libtcmalloc-minimal4 -y
Configuring MySQL/MariaDB with TCMalloc
To preload TCMalloc when MySQL or MariaDB starts, use:
LD_PRELOAD=/usr/lib64/libtcmalloc_minimal.so.4 mysqld
To make the configuration permanent:
sudo systemctl edit mariadb
Add:
[Service] Environment="LD_PRELOAD=/usr/lib64/libtcmalloc_minimal.so.4"
Then restart the service:
sudo systemctl daemon-reexec
sudo systemctl restart mariadb
Comparison between Jemalloc and TCMalloc
| Feature | Jemalloc | TCMalloc |
|---|---|---|
| Multi-threaded efficiency | High | Very high |
| Fragmentation of memory | Bassa | Media |
| Allocation speed | Excellent | Excellent |
| Memory consumption | Slightly higher | Lower |
| Advanced debugging | Yes | No |
If your database suffers from memory fragmentation issues, Jemalloc is the best choice. However, if you need to reduce latency in multi-threaded workloads, TCMalloc may offer superior benefits.
Benchmark JeMalloc TCMalloc Transactions per second TPS
Benchmark tests showed significant performance differences between Jemalloc , TCMalloc , and the standard glibc allocator malloc under various load scenarios and hardware configurations.
- 4 vCPUs: With a limited number of cores, performance was nearly identical for all allocators, with an average throughput of around 2500 TPS (transactions per second).
- 8 vCPUs: The throughput of Jemalloc e TCMalloc has doubled, reaching 5000 TPS, while with glibc malloc a significant decrease was observed in 3500 TPS when the thread count reached 64-128.
- 16 vCPUs: Jemalloc e TCMalloc have maintained a stable increase in throughput up to 6300 TPS up to 4096 threads. On the contrary, with glibc malloc, throughput dropped dramatically after the 16 thread, stabilizing around the 4000 TPS.
- 32 vCPUs: Jemalloc e TCMalloc have shown significant improvement, reaching a peak of 12500 TPS and maintaining high performance up to 1024 thread, with a slight decrease above this threshold. Glybc malloc, instead, showed a drastic drop in performance, with TPS dropping below the levels recorded in the tests at 8 and 16 vCPUs, settling around 3100 TPS.
In summary, in Online Transaction Processing Read-Only (OLTP_RO) tests on a 32-vCPU server , the performance difference between glibc malloc and Jemalloc/TCMalloc was approximately 4x, favoring the advanced allocators. This highlights how, with increasing numbers of concurrent cores and threads, Jemalloc and TCMalloc provide more stable and scalable performance , dramatically reducing the bottlenecks caused by inefficient memory allocation with glibc malloc.
Final houghts
- If the server has 8 cores or less, no significant differences are observed between glibc malloc and alternative allocators.
- If the server has more than 8 cores, it is advisable to try and benchmark with Jemalloc or TCMalloc, as they can significantly improve MySQL performance at no additional cost.
- If you run benchmark tests on multi-core servers, it is essential to enable an alternative memory allocator, otherwise performance will be limited by glibc malloc rather than the MySQL engine itself.
Conclusions
Using an optimized memory allocator like Jemalloc or TCMalloc can significantly improve MySQL and MariaDB performance in production environments, especially under heavy workloads. Jemalloc stands out for its ability to reduce memory fragmentation, ensuring more efficient management and offering advanced debugging tools. TCMalloc, on the other hand, is designed to reduce the latency of allocation and deallocation operations, improving performance in highly parallel, multi-threaded systems.
Benchmarks show that adopting one of these allocators can reduce RAM consumption, improve query throughput, and ensure greater operational stability over time. However, the ideal allocator depends on the type of database workload: Jemalloc is often the best choice for environments where memory management needs to be more predictable and optimized, while TCMalloc is preferable in contexts where latency reduction and allocation speed are priorities.
If your goal is to maximize memory efficiency and improve database scalability, consider migrating to Jemalloc or TCMalloc . After implementation, carefully monitor database performance with benchmarking and memory analysis tools to ensure maximum benefit and tailor the configuration to your specific infrastructure needs.