---
title: "Google perftools 优化mysql  nginx(TCMalloc)"
author: "ssxxjjii"
date: "2011-11-02T15:51:14Z"
tags: []
categories: ["开源软件"]
slug: "google-perftools-mysql-nginx-tcmalloc"
draft: false
lastmod: "2011-11-02T15:51:14Z"
id: 1231077
url: "https://tdcq.iteye.com/blog/1231077"
---

TCMalloc ([google-perftools](http://code.google.com/p/google-perftools/)) 是用于优化C++写的多线程应用，比glibc 2.3的malloc快。这个模块可以用来让MySQL在高并发下内存占用更加稳定。

下载二个软件地址：

[google-perftools](http://control.blog.sina.com.cn/p/google-perftools/)：[http://code.google.com/p/google-perftools/](http://code.google.com/p/google-perftools/)

libunwind：[http://download.savannah.gnu.org/releases/libunwind/](http://download.savannah.gnu.org/releases/libunwind/)

安装步骤：

1.64位操作系统请先安装 libunwind库，32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能，其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。  
#tar zxvf libunwind-0.9.tar.gz  
#cd libunwind-0.99

#./configure  
#make  
#make install

2.安装google-perftools：  
#tar zxvf google-perftools-1.6.tar.gz

#cd google-perftools-1.6

#./configure  
#make  
#make install

3.运行以下二行命令

echo "/usr/local/lib" &gt; /etc/ld.so.conf.d/usr\_local\_lib.conf  
/sbin/ldconfig

4.修改MySQL启动脚本（根据你的MySQL安装位置而定）：

vi /usr/local/mysql/bin/mysqld\_safe

在# executing mysqld\_safe的下一行，加上：

export LD\_PRELOAD=/usr/local/lib/libtcmalloc.so

保存后退出，然后重启MySQL服务器。

 

5.使用验证lsof命令查看tcmalloc是否起效：

如lsof没有装，可yum安装

/usr/sbin/lsof -n | grep tcmalloc  
如果发现以下信息，说明tcmalloc已经起效：  
　　mysqld    10847   mysql  mem       REG        8,5  1203756   20484960 /usr/local/lib/libtcmalloc.so.0.0.0    
\----------------------------------------------------------------------

2011.2.4版本更新为1.7  
http://google-perftools.googlecode.com/files/google-perftools-1.7.tar.gz

 安装过程一样，执行/usr/sbin/lsof -n | grep tcmalloc，显示如下：  
mysqld    4358   mysql  mem       REG        253,0 1800149   

22191975 /usr/local/lib/libtcmalloc.so.0.1.0