[Java] CPU 100% 原因查找解决

CPU 100%肯定是出现死锁,这个时候观察内存还是够用的,但是CPU一直100%,以下几步解决:

1. 找到进程消耗cpu最大的

$top
top - 11:37:34 up 100 days, 23:25,  1 user,  load average: 0.75, 0.60, 0.53
Tasks:  32 total,   3 running,  29 sleeping,   0 stopped,   0 zombie
Cpu(s): 11.6%us,  7.0%sy,  0.0%ni, 81.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.1%st
Mem:   8388608k total,  8381460k used,     7148k free,        0k buffers
Swap:  1999864k total,        0k used,  1999864k free,  4089340k cached

   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
123530 admin      0 -20 5631m 4.0g 102m S 73.2 49.6   1024:13 java

2. 打出进程中线程占用情况:

$top -p 157030 -H
top - 11:39:46 up 100 days, 23:27,  1 user,  load average: 0.63, 0.59, 0.54
Tasks: 228 total,   0 running, 228 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.7%us,  0.3%sy,  0.0%ni, 98.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.2%st
Mem:   8388608k total,  8382160k used,     6448k free,        0k buffers
Swap:  1999864k total,        0k used,  1999864k free,  4089636k cached

   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
158166 admin      0 -20 5631m 4.0g 102m S  1.0 49.6  31:21.02 java
157051 admin      0 -20 5631m 4.0g 102m S  0.3 49.6   1:56.39 java

3. dump文件:

$ jstack 123530 > Desktop/jstack.log

4. 找到#2步的PID对应的16进制

$printf %x 158166
269d6

5. 在#3的dump文件中找到#4对应的线程堆栈(0x269d6):

zhangdeMacBook-Air:~ zhang$ more Desktop/jstack.log
2016-07-17 11:45:20
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode):

"Attach Listener" daemon prio=5 tid=0x00007f9aab82a800 nid=0x580b waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"SocketConnectorIoProcessor-0.2" daemon prio=5 tid=0x00007f9aab43b800 nid=0x7003 runnable [0x00000001699a4000]
   java.lang.Thread.State: RUNNABLE
        at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
        at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:200)
        at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
        at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
        - locked <0x00000001585a8078> (a sun.nio.ch.Util$2)
        - locked <0x00000001585a8088> (a java.util.Collections$UnmodifiableSet)
        - locked <0x00000001585a8028> (a sun.nio.ch.KQueueSelectorImpl)
        at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
        at org.apache.mina.transport.socket.nio.SocketIoProcessor$Worker.run(SocketIoProcessor.java:480)
        at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

6. 结合dump和代码分析问题原因,一般都能找到原因

 

查看进程GC情况

sudo -u admin /opt/java/bin/jstat -gcutil 54358 1000

得到Dump文件

sudo -u admin /opt/java/bin/jmap -dump:format=b,file=/tmp/a.bin 54358

通过工具分析dump文件

posted @ 2016-07-17 11:53  我是小菜鸟  阅读(7689)  评论(0编辑  收藏  举报