9月 05

使用TCMalloc优化内存

安装系统CentOS x86_64mysql 5.1.44,已经在运行。

  网上很多介绍64位安装了libunwind,但是用1.0的总是编译不通过,不找原因了跳过了。直接安装google-perftools-1.8.3,因为是64位所以要加–enable-frame-pointers参数。

# wget http://google-perftools.googlecode.com/files/google-perftools-1.8.3.tar.gz

# tar zxvf google-perftools-1.8.3.tar.gz

# cd google-perftools-1.8.3

# ./configure –enable-frame-pointers

# make

# make install

Google-perftools生效

# echo “/usr/local/lib” > /etc/ld.so.conf.d/usr_local_lib.conf

# /sbin/ldconfig

mysql启动部分添加

# vi /usr/local/mysql/bin/mysqld_safe

在“# executing mysqld_safe”在下面增加“export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

重启mysql服务,查看tcmalloc是否生效。如显示如下说明已经生效。

# lsof -n | grep tcmalloc

mysqld 32480 mysql mem REG 8,2 1916397 412627 /usr/local/lib/libtcmalloc.so.0.2.2

TCMalloc (google-perftools) 是用于优化C++写的多线程应用,比glibc 2.3malloc快,原理参见http://shiningray.cn/tcmalloc-thread-caching-malloc.html。这个模块可以用来让MySQL在高并发下内存占用更加稳定。再nginx中使用参数–with-google_perftools_module可以使用。再redis里也可使用再make 时使用“make USE_TCMALLOC=yes”。