How to Enable TCMalloc in MariaDB on Ubuntu
MariaDB, a popular open-source database, can achieve significant performance improvements by utilizing TCMalloc, a memory allocator optimized for multi-threading. Enabling TCMalloc in Ubuntu is straightforward and can result in faster query execution and reduced latency. Here’s how you can set it up.
Check the current malloc in MariaDB
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'version_malloc_library';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| version_malloc_library | system |
+------------------------+--------+
1 row in set (0.001 sec)
A value of “system” indicates the system default, which is normally malloc. If another library is used, this query will return the name and version of the library.
Install TCMalloc
apt update && apt install libgoogle-perftools-dev
Configure MariaDB to Use TCMalloc
# find the tcmalloc path
find /usr/lib -name "libtcmalloc.*"
/usr/lib/x86_64-linux-gnu/pkgconfig/libtcmalloc.pc
/usr/lib/x86_64-linux-gnu/libtcmalloc.a
/usr/lib/x86_64-linux-gnu/libtcmalloc.so.4.5.9
/usr/lib/x86_64-linux-gnu/libtcmalloc.so.4
/usr/lib/x86_64-linux-gnu/libtcmalloc.so
# load the tcmalloc in SystemD MariaDB service
systemctl edit mariadb
# append this config
[Service]
Environment=LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtcmalloc.so
Restart MariaDB Service
systemctl daemon-reload
systemctl restart mariadb
Verify TCMalloc is Enabled
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'version_malloc_library';
+------------------------+---------------------------+
| Variable_name | Value |
+------------------------+---------------------------+
| version_malloc_library | tcmalloc gperftools 2.9.1 |
+------------------------+---------------------------+
1 row in set (0.001 sec)
Conclusion
By following these steps, you can enable TCMalloc in MariaDB on Ubuntu, thus enhancing the database’s performance and speed. This optimization can lead to a smoother and more responsive application, making it a worthwhile tweak for high-demand environments.
Ensure to monitor your database performance and make further adjustments as needed. Happy optimizing!