February 2 2025

Improve MySQL and MariaDB performance with memory allocators like Jemalloc and TCMalloc

Learn how to improve MySQL and MariaDB performance by optimizing memory allocation with Jemalloc and TCMalloc, reducing latency and fragmentation to maximize efficiency.

Jemalloc-TCMalloc-MySQL-MariaDB-Performance-Malloc

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 such as Jemalloc e 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 distributions based on SoulLinux e RHEL, 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 has memory fragmentation problems, Jemalloc is the best choice. If you need to reduce latency in multi-threaded workloads, TCMalloc can offer superior benefits.

Benchmark JeMalloc TCMalloc Transactions per second TPS

Benchmark tests showed significant performance differences between Jemalloc, TCMalloc and the standard allocator glibc malloc under different load scenarios and hardware configurations.

malloc_test2_oltp_ro1

  • 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 short, in the tests OLTP_RO (Online Transaction Processing Read-Only) on a server with 32 vCPUs, the difference in performance between glibc malloc e Jemalloc/TCMalloc It turned out to be approximately 4 times higher in favor of advanced allocators. This highlights how, with an increasing number of concurrent cores and threads, Jemalloc and TCMalloc ensure more stable and scalable performance, dramatically reducing 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 o TCMalloc can significantly improve the performance of MySQL and MariaDB 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. On the other hand, TCMalloc It 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 choice of the ideal allocator depends on the type of database workload: Jemalloc It is often the best choice for environments where memory management needs to be more predictable and optimized, while TCMalloc It 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 o TCMalloc. After implementation, carefully monitor database performance with benchmarking and memory analysis tools to ensure maximum benefit and to tailor the configuration to the specific needs of your infrastructure.

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

INFORMATION

Managed Server Srl is a leading Italian player in providing advanced GNU/Linux system solutions oriented towards high performance. With a low-cost and predictable subscription model, we ensure that our customers have access to advanced technologies in hosting, dedicated servers and cloud services. In addition to this, we offer systems consultancy on Linux systems and specialized maintenance in DBMS, IT Security, Cloud and much more. We stand out for our expertise in hosting leading Open Source CMS such as WordPress, WooCommerce, Drupal, Prestashop, Joomla, OpenCart and Magento, supported by a high-level support and consultancy service suitable for Public Administration, SMEs and any size.

Red Hat, Inc. owns the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. owns the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt. Oracle Corporation owns the rights to Oracle®, MySQL®, and MyRocks®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; REDIS® is a registered trademark of Redis Labs Ltd. F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB. Adobe Inc. holds the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited. Automattic Inc. owns the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert holds the rights to Drupal®. Amazon Web Services, Inc. holds the rights to AWS®; Google LLC holds the rights to Google Cloud™ and Chrome™; Microsoft Corporation holds the rights to Microsoft®, Azure®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®. Apache® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group. CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV Hetzner Online GmbH owns the rights to Hetzner®; OVHcloud is a registered trademark of OVH Groupe SAS; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Facebook, Inc. owns the rights to Facebook®. This site is not affiliated, sponsored or otherwise associated with any of the entities mentioned above and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. Any other trademarks mentioned belong to their registrants. MANAGED SERVER® is a trademark registered at European level by MANAGED SERVER SRL, Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

Back to top