Linux Debugging (九) 一次生产环境下的“内存泄露”

一个偶然的机会,发现一个进程使用了超过14G的内存。这个进程是一个RPC server,只是作为中转,绝对不应该使用这么多内存的。即使并发量太多,存在内存中的数据太多,那么在并发减少的情况下,这个内存使用肯定会降下来。但是事实上,这个内存会一直涨,直到被OOM Killer杀掉。

由于这个rpc server的逻辑比较简单,先走读源码,除了发现一些简单的编程上面的问题外,没有大的问题。先上valgrind:

valgrind --tool=memcheck --leak-check=full -v ./rpc_server

原来的情况是,一般都会检查出内存泄露的。这次没有(否则也不会有本文了):

实际上至少10G的“内存泄露”。既然没有检查出,说明这些内存还是活着的。设想一下这个场景:每个请求都new一块内存,放到一个列表中。正常的话请求处理完需要从这个列表中删除这块内存。如果没有删除,那么这就算是内存泄露。但是valgrind检查不出来。

由于上面这个进程使用了tcmalloc,是不是tcmalloc的问题?我们知道tcmalloc的效率要优于malloc,那么是不是tcmalloc的问题,如果它一直申请内存,不释放,就会造成这种”内存泄露“。注意下面一段话:

Releasing Memory Back to the System

By default, tcmalloc will release no-longer-used memory back to the kernel gradually,
over time.
The tcmalloc_release_rate flag controls how quickly this happens.
You can also force a release at a given point in the progam execution like so:

   MallocExtension::instance()->ReleaseFreeMemory();
You can also call SetMemoryReleaseRate() to change the tcmalloc_release_rate value
 at runtime, or GetMemoryReleaseRate to see what the current release rate is.

简单翻译一下,就是tcmalloc将内存交回OS的机制:默认情况下,tcmalloc会将长时间未用的内存交还系统。tcmalloc_release_rate这个flag控制了这个交回频率。你可以在运行时通过这个语句强制这个release发生:

 MallocExtension::instance()->ReleaseFreeMemory();

当然了,你可以通过SetMemoryReleaseRate() 来设置这个tcmalloc_release_rate. 如果设置为0,代表永远不交回。数字越大代表交回的频率越大。一般合理的值就是设置一个0 - 10 之间的一个数。也可以通过设置环境变量TCMALLOC_RELEASE_RATE来设置这个rate。

带着这个怀疑,首先还是通过Google‘s gpreftools检查一下heap的使用情况:

1.  export HEAPCHECK=draconian

2.  export PPROF_PATH=/usr/local/bin/pprof

直接启动即可。

之所以设置为draconian,因为想得到更详细的统计信息。更将相信的解释如下:Flavors of Heap Checking

These are the legal values when running a whole-program heap check:

  1. minimal
  2. normal
  3. strict
  4. draconian

"Minimal" heap-checking starts as late as possible in a initialization, meaning you can leak some memory in your initialization routines (that run before main(), say), and not trigger a leak message. If you frequently (and purposefully) leak data in one-time global initializers, "minimal" mode is useful for you. Otherwise, you should avoid it for stricter modes.

"Normal" heap-checking tracks live objects and reports a leak for any data that is not reachable via a live object when the program exits.

"Strict" heap-checking is much like "normal" but has a few extra checks that memory isn‘t lost in global destructors. In particular, if you have a global variable that allocates memory during program execution, and then "forgets" about the memory in the global destructor (say, by setting the pointer to it to NULL) without freeing it, that will prompt a leak message in "strict" mode, though not in "normal" mode.

"Draconian" heap-checking is appropriate for those who like to be very precise about their memory management, and want the heap-checker to help them enforce it. In "draconian" mode, the heap-checker does not do "live object" checking at all, so it reports a leak unless all allocated memory is freed before program exit. (However, you can use IgnoreObject() to re-enable liveness-checking on an object-by-object basis.)

"Normal" mode, as the name implies, is the one used most often at Google. It‘s appropriate for everyday heap-checking use.

In addition, there are two other possible modes:

  • as-is
  • local

as-is is the most flexible mode; it allows you to specify the various knobs of the heap checker explicitly. local activates the explicit heap-check instrumentation, but does not turn on any whole-program leak checking.

但是很不幸,还是没有检查出来:

上面的泄露统计不是预期的,因为“泄露”了至少10G的内存了。

那么还是强制的释放不用的buffer吧:

MallocExtension::instance()->ReleaseFreeMemory();

问题解决了。

时间: 2024-08-26 08:01:07

Linux Debugging (九) 一次生产环境下的“内存泄露”的相关文章

只需4个步骤,分析解决在生产环境下JVM内存泄露问题

作者:未完成交响曲 发现异常 首先通过我们内部搭建的日志平台发现我们线上环境一个java应用有大量的http接口请求超时,登录linux服务器查看网络环境没有问题,判断是应用自身运行异常,重启应用后发现异常还在,开始查找问题. 初步查找问题 通过指令:jstat -gcutil 查看jvm内存占用和gc情况: 发现老年代内存占用比例过高,并且每次fullGC后并没有有效回收.老年代内存占用百分比变化趋势大致如下: 初步判断大量请求超时和服务瘫痪的直接原因:每次fullGC后的内存占用越来越高内存

生产环境下的iptables

生产环境下的iptables设置,这是我自己的一点总结,浅显之处望大家指出批评,共同学习. 我的局域网为192.168.1.0/24. 1.先清空所有规则 iptables -F iptables -X iptables -Z iptables -t nat -F iptables -t nat -X iptables -t nat -Z 设置默认规则前开发ssh(6123)端口 iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --dport

[原]生产环境下的nginx.conf配置文件(多虚拟主机)

[原]生产环境下的nginx.conf配置文件(多虚拟主机) 2013-12-27阅读110 评论0 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在自己的机器上使用了,本配置文件摘录自<构建高可用Linux服务器>(机械工业出版社),转载麻烦注明出处,谢谢,配置文件如下: user  www www;worker_processes 8;error_log  /data/logs/nginx_error.log  crit;pid      

SpringCloud从入门到进阶(四)——生产环境下Eureka的完全分布式部署

内容 由于前两节的内容我们知道,开启了preferIpAddress后,Eureka的伪分布式部署会提示replica不可用.这一节我们讲解如何在生产环境下部署完全分布式的Eureka集群,确保开启了preferIpAddress后replica的可用性. 版本 IDE:IDEA 2017.2.2 x64 JDK:1.8.0_171 manve:3.3.3 SpringBoot:1.5.9.RELEASE SpringCloud:Dalston.SR1 适合人群 Java开发人员 节点信息: 节

生产环境下ftp的迁移并构建高可用

说明:这是1个小项目就两台DELL的服务器,和一台IP SAN存储(DELL MD3200i).原来是4台小服务器,而且服务器太老了,经常有问题,这回相当于一次ftp的迁移,以前用的是proftp,这次换成了vsftp.数据量有2.5T. 拓扑很简单: 系统:CENTOS 6.4(64bit) 高可用软件:corosync+pacemaker host:ftp1 192.168.1.190 ftp2  192.168.1.191 stonith(ipmi):ftp1 192.168.1.180

读生产环境下go语言最佳实践有感

最近看了一篇关于go产品开发最佳实践的文章,go-in-procution.作者总结了他们在用go开发过程中的很多实际经验,我们很多其实也用到了,鉴于此,这里就简单的写写读后感,后续我也争取能将这篇文章翻译出来.后面我用soundcloud来指代原作者. 开发环境 在soundcloud,每个人使用一个独立的GOPATH,并且在GOPATH直接按照go规定的代码路径方式clone代码. $ mkdir -p $GOPATH/src/github.com/soundcloud $ cd $GOPA

生产环境下was不允许重启,怎么办?

前段时间上线,遇到一个jndi的故障问题,怎么个问题呢?就是原在测试环境下没有问题,而在生产环境下无法连接生产数据库,当时找到问题所在,就是ibm工具自动生成一个在测试环境下连接的jndi的资源文件resources.xml,当时删除了,重启了server,无效.后来我考虑到这肯定是was缓存造成,因此想象缓存造成的原因,最后在测试环境下重启了was,问题解决了,但后来说生产环境是不可能重启was的,因此暂时困老了本人,后来所谓的领导说,他去找总架构师看有没有办法解决,可是时间不等人,过了2天依

EF 第三篇 生产环境下的数据迁移

前言 本文所谓数据迁移,直白点不如说成数据库升级.虽然大部分带服务器型的应用,所有客户端都是连到同一台服务器上,对这样的生产环境,数据库升级起来不是什么难事,用vs自带的Migration也好,执行sql脚本也好,都比较容易.然而在每家客户现场都要部署一台服务器的应用也不少,如果一家家手工地去升级数据库,那将是一个可怕的工作量.那么对于这样的环境要怎么做到自动升级数据库呢?相信大家也在网上搜了不少了EF关于生产环境下的数据迁移方案,然后99%搜到的都是使用vs自带的Migration命令方式迁移

Windows 2008下 rman backup scirpts(备份脚本)--已经在生产环境下通过验证

一.任务计划 windows -控制面板-管理工具-计划任务程序,右击"任务计划程序库","创建任务" 二:相关脚本中的内容: 2.1  back_rman.bat中的内容: set ORACLE_SID=FS F:\oracle\product\10.2.0\db_1\BIN\rman target / cmdfile=H:\worksql\windows_backupscript\backup.rcv log=H:\worksql\windows_backups