使用google-perftools优化nginx内存管理提升性能
一、前言#
1、使用google开发的google-perftools优化nginx的内存分配效率和速度,帮助在高并发的情况下控制内存的使用。
2、TCMalloc在内存的分配上效率和速度要比malloc高得多。但是nginx的内存占用其实是很少的,一个进程占用的内存大概只有12M左右,所有google-perftools对nginx的优化效果可能不太明显。
二、安装#
1、下载并安装google-perftools
# 从github上下载perftools工具包
# https://github.com/gperftools/gperftools
shell> cd /usr/local/src/
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的值有关
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优化安装完成。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?