分析java線程CPU佔用率過高的方法和步驟:
1) 查看CPU佔用率TOP10 的PID
[root@qipsuit1 ~]# ps -aux | sort -k3nr | head -10Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQroot 30428 202 1.3 12513592 430904 pts/7 Sl 14:20 135:35 java -Djava.ext.dirs=/qip/run/app/qip-collector/ -jar
2) 顯示線程列表,並按照CPU佔用高的線程排序
[root@qipsuit1 ~]# ps -mp 30428 -o THREAD,tid,time | sort -rnUSER %CPU PRI SCNT WCHAN USER SYSTEM TID TIMEroot 99.9 19 - - - - 30932 01:08:18root 99.8 19 - - - - 30933 01:08:18
3) 將需要的線程ID轉換為16進位格式
[root@qipsuit2 ~]# printf "%x\n" 3093278d4[root@qipsuit1 ~]# printf "%x\n" 3093378d5
4) 查看stack
[root@qipsuit1 ~]# jstack 30428 |grep 78d4 -A 30"Thread-15" prio=10 tid=0x00007fa28c058800 nid=0x78d4 runnable [0x00007fa3a8e49000] java.lang.Thread.State: RUNNABLE at com.gtstar.collector.thread.MsgSaveThread.run(MsgSaveThread.java:68) at java.lang.Thread.run(Thread.java:745)"Thread-13" prio=10 tid=0x00007fa28c056800 nid=0x78d3 waiting on condition [0x00007fa3a8f4a000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at com.gtstar.collector.thread.MemoryMsgSaveThread.run(MemoryMsgSaveThread.java:33) at java.lang.Thread.run(Thread.java:745)
[root@qipsuit1 ~]# jstack 30428 |grep 78d5 -A 30"Thread-17" prio=10 tid=0x00007fa28c05b000 nid=0x78d5 runnable [0x00007fa3a8d48000] java.lang.Thread.State: RUNNABLE at com.gtstar.collector.thread.AckMsgSaveThread.run(AckMsgSaveThread.java:66) at java.lang.Thread.run(Thread.java:745)"Thread-15" prio=10 tid=0x00007fa28c058800 nid=0x78d4 runnable [0x00007fa3a8e49000] java.lang.Thread.State: RUNNABLE at com.gtstar.collector.thread.MsgSaveThread.run(MsgSaveThread.java:68) at java.lang.Thread.run(Thread.java:745)
注意看紅色部分