0%

优化MySQL内存管理:tcmalloc

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

安装依赖包

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

下载tcmalloc DownLoad

解压安装

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

配置软链接

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

修改mysqld_safe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ 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

重启数据库

1
$ /etc/init.d/mysqld restart

查看tcmalloc是否应用

1
2
3
4
5
$ 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