使用google-perftools优化nginx内存管理提升性能

一、前言

1、使用google开发的google-perftools优化nginx的内存分配效率和速度,帮助在高并发的情况下控制内存的使用。

2、TCMalloc在内存的分配上效率和速度要比malloc高得多。但是nginx的内存占用其实是很少的,一个进程占用的内存大概只有12M左右,所有google-perftools对nginx的优化效果可能不太明显。

二、安装

1、下载并安装google-perftools

# 从github上下载perftools工具包 shell> wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.7/gperftools-2.7.tar.gz # 注意: # 1、如果是32位系统,在configure gperftools的时候,可以不添加--enable-frame-pointers参数。 # 2、如果是64位系统,需要先安装libunwind,再在configure gperftools的时候,添加--enable-frame-pointers参数。 shell> yum install epel-release -y shell> yum install libunwind-devel -y shell> tar zxvf gperftools-2.7.tar.gz shell> cd gperftools-2.7 shell> ./configure --enable-frame-pointers --enable-libunwind --with-tcmalloc-pagesize=32 shell> make && make install

2、使nginx支持google-perftools

# 重新编译ngx,添加--with-google_perftools_module # 简单演示参数添加方法,详细的nginx安装过程,请参考《源码安装openresty》 shell> ./configure --with-google_perftools_module shell> make && make install

3、配置nginx对google-perftools的支持

# 添加线程目录 shell> mkdir /tmp/tcmalloc shell> chmod 0777 /tmp/tcmalloc # 修改nginx.conf,在pid行下面添加如下信息 google_perftools_profiles /tmp/tcmalloc; # 重启nginx shell> /opt/openresty/nginx/sbin/nginx -s reload # 验证是否支持 shell> lsof -n |grep tcmalloc nginx 1414 nobody 12w REG 252,1 0 1452011 /tmp/tcmalloc.1414 nginx 1415 nobody 14w REG 252,1 0 1452010 /tmp/tcmalloc.1415 # 备注:tcmalloc的记录文件数量与nginx.conf中worker_processes的值有关,每个worker对应一个tcmalloc文件。

4、可能会遇到的问题

# 启动nginx,发现缺少libprofiler.so.0动态库的支持 shell> /opt/openresty/nginx/sbin/nginx /opt/openresty/nginx/sbin/nginx: error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory # 查找系统下是否有libprofiler.so.0动态库 shell> whereis libprofiler.so.0 libprofiler.so: /usr/local/lib/libprofiler.so.0 /usr/local/lib/libprofiler.so # 由于不在nginx程序查找的目录下,所以需要创建软链接 # 评估缺少的动态库支持,可通过ldd /opt/openresty/nginx/sbin/nginx的方式,查看"not found"部分。 shell> ln -s /usr/local/lib/libprofiler.so.0.4.18 /lib64/libprofiler.so.0

至此,nginx下的google-perftools优化安装完成。

作者:龍龍小宝

原文:https://www.cnblogs.com/91donkey/p/11513175.html