Disabling the Transparent Huge Page Function and Using TCMalloc
Purpose
TCMalloc is an efficient memory allocator. It improves the efficiency and performance of memory allocation through a series of optimizations.
- When traditional malloc is allocating and releasing memory, memory fragments occur causing decreased memory usage and insufficient memory. TCMalloc uses fixed-size pages and objects to obtain and allocate memory, reducing memory fragments and improving memory usage.
- TCMalloc applies to multiple-core scenarios and provides stronger support in parallelism. It can set the cache size based on each thread or each CPU to improve parallel performance.
- TCMalloc improves the speed of obtaining memory by caching common objects.
- TCMalloc provides independent cache allocation policy for each thread to reduce lock contention between multiple threads and further optimizes memory allocation efficiency and performance.
Procedure
- Disable the transparent huge page (THP) function.
1 2
echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag
- Use TCMalloc.
The following uses MariaDB as an example to describe how to use TCMalloc. In the following commands, replace gperftools-xx with the actual gperftools version.
- Download the gperftools source code.
Download address: https://github.com/gperftools/gperftools/releases
- Decompress the gperftools source package.
1tar -zxvf gperftools-xx.tar.gz
- Go to the gperftools source code directory and perform compilation and installation.
1 2 3 4
cd gperftools-xx ./configure --enable-frame-pointers make -j make install
- Modify the mysqld_safe file, which is the MariaDB startup script. /usr/local/mysql/bin/mysqld_safe indicates the directory of the mysqld_safe file. Replace the directory with the actual one.
- Open the mysqld_safe file.
1vim /usr/local/mysql/bin/mysqld_safe - Press i to enter the insert mode. Locate the line under #executing mysqld_safe and add the following content:
1export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Restart the MariaDB service for the modification of mysqld_safe to take effect.
- Open the mysqld_safe file.
- Download the gperftools source code.
Parent topic: OS Tuning