一、问题
最近生产环境的一个后端应用老是出现内存溢出OutOfMemory,然后导致整个服务不可用的问题。
二、背景
(1)后端应用
springMVC的应用,原来是jsp单体的老应用,后来改造成一个后端服务。
其实就是增加了一个mobile的package,里面放了一些移动相关的接口,其他的不动(老项目都这样,复制一下拿来用),然后部署在weblogic服务器上,为移动端请求提供接口服务。
(2)前端应用
移动端是H5页面,然后外面包一层安卓的壳。
(3)登录认证问题
登录认证的前后端交互没有通过cookie和session共同作用的
登录认证简单来说就是:
(针对单次请求)
1.移动端请求?userid=xxx
2.后端接收到请求,根据userid获取用户信息
3.getSession(),然后把用户信息放进session里
4.后面这个请求执行一系列复杂的业务代码,可能会在某一个步骤需要获取用户信息
5.从session里面读取用户信息
6.请求结束,session便没用了
问题所在:getSession()每次都会创建一个新的session对象,而且使用完并没手动session.invalidate()使session失效,只能等session超时失效后被jvm垃圾回收。
session对象越积越多,所以查看dump文件时可以看到weblogic.servlet.internal.session.MemorySessionContext对象占用了40%
三、问题解决过程
(1)服务老是挂掉,而且经常挑周末挂,周末睡得好好的,突然用户群就炸了,这让运维妹子苦不堪言。
(2)之前有大概看过日志,模糊记得是内存溢出导致的。于是,便开始着手准备排查。
(3)生成dump文件 (-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:\dump)
在startWeblogic.cmd启动文件中的set USER_MEM_ARGS末尾加上-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:\dump
然后重启。
(4)静静等待服务器再次挂掉【手动狗头】
(5)果不其然,过了几天挂掉了,dump出一个2个猪的文件
(6)文件太大了,qq和公司邮箱没法发,只能叫她上传到百度云盘我再下载下来,顺带体验了一把“用闲置宽带为下载提速”
(7)下载回来用MemoryAnalyzer打开
第一次打开失败,原因是文件太大了,导致软件崩了。后来调大MemoryAnalyzer的运行内存:MemoryAnalyzer.ini里的-Xmx改成了2048m,终于顺利打开了
(8)分析问题
看来罪魁祸首就是它了,但是这是个什么玩意呢?
(9)寻求度娘帮助
网上相关的资料不多,后来终于找到一篇
这里是原文:http://www.voidcn.com/article/p-etbhmwaj-ee.html
引用一下作者原文:
APPLIES TO:
Oracle WebLogic Server - Version 10.3.5 and later
Information in this document applies to any platform.
SYMPTOMS
There is huge consumption of memory in the production environment. A memory dump shows two WebLogic classes as leak suspects:
- weblogic.servlet.internal.session.MemorySessionContext (66.05%)
- weblogic.servlet.internal.session.MemorySessionData (21.52%)
CAUSE
The "session-timeout" value in in web.xml is set to 1 hour (3600 seconds) by default. If the session is not invalidated or timed out in application code, each session will live for that full hour, causing objects to accumulate until the container times the session out. Combine this with heavy traffic (so lots of sessions opened), and this causes huge memory consumption.
To find if there are any invalid sessions lingering in the heap after a particular time, it is recommended that the application explicitly call session.invalidate()
to invalidate a session after the user logs out. However WLS also provides a mechanism to expire sessions after a specific interval that is referred to as the "session-timeout" parameter in the web.xml. The value set in this element overrides the value set in the TimeoutSecs attribute of the element in the WebLogic-specific deployment descriptor weblogic.xml. If nothing is specified the default value of 3600 seconds is used. See [Session Timeout](javascript:void()) for more information.
SOLUTION
To reduce the memory consumption from HTTP session objects, please follow these steps:
- Ensure that the application code explicitly calls
session.invalidate()
when a user logs out of their session. - Lower the "session-timeout" value set in in web.xml to (for example) 15 minutes.
大致说的是:weblogic 中的session-timeout配置默认是一个小时,意思是session对象默认一个小时后才会过期,新的不断产生,旧的还没过期,导致越来越多的session对象存活在内存中,最终导致了内存溢出。
四、解决办法
结合本项目的实际情况(每个请求都会产生新的session对象),而且项目中并没有看到有代码显示的使session对象失效的代码session.invalidate()
,我猜想可能是session过期时间过长导致的。
1.首先查看一下本地项目代码web.xml中session-timeout,我擦!居然是5400分钟,一个半小时
2.那非常有可能就是它的锅了,于是叫现场的同事检查一下生产环境配置,果然,现场的配置也是5400
3.根据项目的实际情况,我叫现场同事把生产环境配置改成10分钟,此问题应该可以得以解决。
观察一段时间后再补充结果。
五、性能提升
条件:
1.每次请求都产生一个新的session对象
2.session对象过期时间为5400分钟
3.假设每个用户每分钟能发送100个请求
dump文件中显示内存溢出的时候,内存中共有840000000多个对象
840000000÷5400÷100≈1555
共能支撑大约1555个用户同时在线
优化后
(840000000÷5400÷100)*540=840000
注:总用户数才10000左右
是不是可以吹一波牛逼说,理论上性能提升了540倍呢?【手动狗头】
至于为什么要改成10,不改成5或者1,跟项目有一定关系:项目非常老旧,而且各种调用,甚至还会去调用oracle的存储过程,保不准会超过1分钟,改成5分钟其实理论上已经很稳了,但是为了防止会有奇奇怪怪的现象发生,还是改成10求稳。而且改后能支撑的用户数理论上已经远远超过了总用户数。
六、总结
1.学会分析dump文件
2.结合项目实际情况定位问题和解决问题
文中如有不准确的地方,还望各位批评指正。感谢!