默认情况下,MySQL内存管理使用的是glibc中的malloc(ptmalloc),实际效果并不是很理想,内存占用率和性能都相对较差。而tcmalloc是由google开源的gperftools其中一个组件,对比默认的malloc来说是一种更好的选择,当然也可以选择MariaDB和redis都采用的jemalloc。项目地址

安装依赖包

$ yum install -y gcc-g++ make libtool libunwind

下载tcmalloc DownLoad

解压安装

$ tar -xvf gperftools-2.8.tar.gz
$ cd gperftools-2.8
$ ./autogen.sh
$ ./configure --enable-frame-pointers
$ make && make install -j 4

配置软链接

$ ln -sf /usr/local/lib/libtcmalloc.so /usr/lib/
$ ln -sf /usr/local/lib/libtcmalloc.so /usr/lib/

修改mysqld_safe

$ vi /usr/local/mysql/bin/mysqld_safe
#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind
#
# Script to start the MySQL daemon and restart it if it dies unexpectedly
#
# This should be executed in the MySQL base directory if you are using a
# binary installation that is not installed in its compile-time default
# location
#
# mysql.server works by first doing a cd to the base directory and from there
# executing mysqld_safe
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

重启数据库

$ /etc/init.d/mysqld restart

查看tcmalloc是否应用

$ lsof |grep -i libtcmalloc.so
mysqld 112616 mysql mem REG 253,0 2588008 1459281 /usr/local/lib/libtcmalloc.so.4.5.5
mysqld 112616 112618 mysql mem REG 253,0 2588008 1459281 /usr/local/lib/libtcmalloc.so.4.5.5
mysqld 112616 112619 mysql mem REG 253,0 2588008 1459281 /usr/local/lib/libtcmalloc.so.4.5.5
mysqld 112616 112620 mysql mem REG 253,0 2588008 1459281 /usr/local/lib/libtcmalloc.so.4.5.5