由于公司网站是用tomcat发布java开发的系统,但是前段时间我发现该系统内存在不断的增加,于是想解决这个问题:
1. top命令查看CPU和内存占用率
top回车,然后按1
发现进程PID 35163 CPU和内存占用率都很高
top - 06:13:47 up 5:31, 1 user, load average: 2.11, 2.07, 2.06
Tasks: 189 total, 1 running, 188 sleeping, 0 stopped, 0 zombie
Cpu0 : 22.3%us, 0.7%sy, 0.0%ni, 76.4%id, 0.3%wa, 0.3%hi, 0.0%si, 0.0%st
Cpu1 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 8.0%us, 0.3%sy, 0.0%ni, 91.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 70.4%us, 1.7%sy, 0.0%ni, 25.6%id, 2.3%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1012292k total, 943076k used, 69216k free, 9372k buffers
Swap: 1675260k total, 375204k used, 1300056k free, 34024k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
35163 user111 20 0 3823m 360m 6828 S 199.5 36.5 147:41.46 java
另外,top下,按H可以在进程PID和线程TID之间切换
2. 查看PID对应的进程
[user111@kms210 lms]$ ps -ef|grep 35163
user111 35163 1 99 04:59 pts/0 02:28:06
/usr/local/jdk1.8.0_60/bin/java
-Djava.util.logging.config.file=/usr/local/lms/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.awt.headless=true -Djava.endorsed.dirs=/usr/local/lms/endorsed
-classpath
/usr/local/lms/bin/bootstrap.jar:/usr/local/lms/bin/tomcat-juli.jar
-Dcatalina.base=/usr/local/lms -Dcatalina.home=/usr/local/lms
-Djava.io.tmpdir=/usr/local/lms/temp
org.apache.catalina.startup.Bootstrap start
user111 44605 23049 0 06:14 pts/0 00:00:00 grep 35163
3. 查看线程35163中占用CPU高的进程
[user111@kms210 lms]$ ps -mp 35163 -o THREAD,tid,time
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
user111 199 - - - - - - 02:32:04
user111 0.0 19 - futex_ - - 35163 00:00:00
user111 0.0 19 - poll_s - - 35164 00:00:01
user111 0.0 19 - futex_ - - 35165 00:00:01
user111 0.0 19 - futex_ - - 35166 00:00:00
user111 0.0 19 - futex_ - - 35167 00:00:00
user111 0.0 19 - futex_ - - 35168 00:00:00
user111 0.3 19 - futex_ - - 35169 00:00:16
user111 0.3 19 - futex_ - - 35170 00:00:16
user111 0.1 19 - futex_ - - 35171 00:00:05
user111 0.0 19 - futex_ - - 35172 00:00:00
user111 0.0 19 - futex_ - - 35173 00:00:01
user111 0.0 19 - futex_ - - 35174 00:00:00
user111 0.0 19 - futex_ - - 35178 00:00:00
user111 0.0 19 - ep_pol - - 35181 00:00:00
user111 0.0 19 - ep_pol - - 35182 00:00:00
user111 0.0 19 - ep_pol - - 35183 00:00:00
user111 99.6 19 - - - - 35204 01:15:33
user111 0.0 19 - futex_ - - 35222 00:00:00
user111 99.6 19 - - - - 35226 01:15:27
user111 0.0 19 - futex_ - - 35230 00:00:00
线程TID转16进制
[user111@kms210 lms]$ printf "%x\n" 35204
8984
4.查看线程栈信息
[user111@kms210 lms]$ jstack 35163 | grep 8984 -A 30
"Thread-6" #18 daemon prio=5 os_prio=0 tid=0x00007fb0d8fd1800 nid=0x8984 runnable [0x00007fb11cc8e000]
java.lang.Thread.State: RUNNABLE
at com.qsd.sssm.thread.LmsThread.run(LmsThread.java:68)
"NioBlockingSelector.BlockPoller-3" #15 daemon prio=5 os_prio=0 tid=0x00007fb130531800 nid=0x896f runnable [0x00007fb11dfb2000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x00000000f62278e8> (a sun.nio.ch.Util$2)
- locked <0x00000000f62278f8> (a java.util.Collections$UnmodifiableSet)
- locked <0x00000000f62278a0> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:342)
"NioBlockingSelector.BlockPoller-2" #14 daemon prio=5 os_prio=0 tid=0x00007fb130517000 nid=0x896e runnable [0x00007fb11e0b3000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x00000000f6227b20> (a sun.nio.ch.Util$2)
- locked <0x00000000f6227b30> (a java.util.Collections$UnmodifiableSet)
- locked <0x00000000f6227ad8> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:342)
"NioBlockingSelector.BlockPoller-1" #13 daemon prio=5 os_prio=0 tid=0x00007fb130474800 nid=0x896d runnable [0x00007fb11e3cd000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
[user111@kms210 lms]$
找到出现问题的代码,并分析具体函数中是否有可能出现死循环的代码段。
通常问题出现在while, for之类的循环代码片段。
5. 查看文件LmsThread.java:68行
Debug一下,发现死循环
while (true) {
if(CMD.remoteIp.equals("")) {// 第58行,if成立,死循环
continue;
}
6. 另外,只替换.war文件可能存在的问题
修改只替换.war文件,可能会发现线程占用率还是很高,线程栈信息仍然显示以前的行号的话,可以试试完整的删除项目文件夹,重新部署整个项目
参考:http://blog.csdn.net/five3/article/details/28416771