Rate This Document
Findability
Accuracy
Completeness
Readability

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.

    1. Download the gperftools source code.

      Download address: https://github.com/gperftools/gperftools/releases

    2. Decompress the gperftools source package.
      1
      tar -zxvf gperftools-xx.tar.gz
      
    3. 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
      
    4. 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.
      1. Open the mysqld_safe file.
        1
        vim /usr/local/mysql/bin/mysqld_safe
        
      2. Press i to enter the insert mode. Locate the line under #executing mysqld_safe and add the following content:
        1
        export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
        
      3. Press Esc, type :wq!, and press Enter to save the file and exit.
      4. Restart the MariaDB service for the modification of mysqld_safe to take effect.