java进程CPU高分析

JVM导致系统CPU高的常见场景:

内存不足,JVM gc频繁,一般会伴随OOM
JVM某个线程死循环或者递归调用

定位和解决
1.内存不足,gc频繁可参考我的这遍文章解决。https://blog.csdn.net/moranzi1/article/details/88670204
2.JVM某个线程死循环或者递归调用。这种情况关键是找到导致CPU高的线程。然后根据具体线程具体分析为什么该线程会导致CPU高。需要线程的步骤如下。


top——命令查看cpu高的进程

[root@iZwz9gs2zseivevv1k5vnkZ syp]# top
top - 11:58:57 up 84 days, 15:19, 6 users, load average: 4.59, 4.16, 3.26
Tasks: 262 total, 1 running, 261 sleeping, 0 stopped, 0 zombie
%Cpu(s): 84.1 us, 4.5 sy, 0.0 ni, 10.8 id, 0.1 wa, 0.0 hi, 0.6 si, 0.0 st
KiB Mem : 16266412 total, 198808 free, 15154212 used, 913392 buff/cache
KiB Swap: 1049596 total, 16848 free, 1032748 used. 460116 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 
12107 root 20 0 4528084 978784 7384 S 293.0 6.0 68:23.05 java 
16699 root 20 0 4281608 514176 0 S 15.9 3.2 11778:45 java 
8193 root 20 0 4349112 813096 0 S 12.0 5.0 12573:55 java 
30793 root 20 0 4405120 855476 7336 S 2.3 5.3 8:16.27 java 
10230 root 20 0 4508844 781092 7192 S 2.0 4.8 6:26.20 java 
17192 root 10 -10 137084 12684 2508 S 2.0 0.1 696:57.96 AliYunDun

ps aux | grep PID——命令查看具体进程信息


ps -mp pid -o THREAD,tid,time——命令查看进程线程信息,找到导致CPU高的线程TID

[root@iZwz9gs2zseivevv1k5vnkZ syp]# ps -mp 12107 -o THREAD,tid,time
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
root 22.7 - - - - - - 00:42:58
root 0.0 19 - futex_ - - 12107 00:00:00
root 0.1 19 - futex_ - - 12108 00:00:19
root 4.6 19 - - - - 12109 00:08:50
root 4.6 19 - - - - 12110 00:08:50
root 4.6 19 - - - - 12111 00:08:48
root 4.6 19 - - - - 12112 00:08:49
root 1.2 19 - futex_ - - 12113 00:02:26

printf "%x " tid——命令转换线程为16进制格式

[root@iZwz9gs2zseivevv1k5vnkZ syp]# printf “%x
” 12109
“2f4dn”

[root@iZwz9gs2zseivevv1k5vnkZ syp]# printf “%x
” 12110
“2f4en”

[root@iZwz9gs2zseivevv1k5vnkZ syp]# printf “%x
” 12111
“2f4fn”

[root@iZwz9gs2zseivevv1k5vnkZ syp]# printf “%x
” 12112
“2f50n”

jstack pid |grep tid -A 30——命令查看线程信息,定位到具体线程

[root@iZwz9gs2zseivevv1k5vnkZ syp]# jstack 12107 | grep 2f4d -A 30
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f223405e000 nid=0x2f4d runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f2234060000 nid=0x2f4e runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f2234061800 nid=0x2f4f runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f2234063800 nid=0x2f50 runnable

"VM Periodic Task Thread" os_prio=0 tid=0x00007f22341ed800 nid=0x2f59 waiting on condition

JNI global references: 1925

[root@iZwz9gs2zseivevv1k5vnkZ syp]# jstack 12107 | grep 2f4e -A 30
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f2234060000 nid=0x2f4e runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f2234061800 nid=0x2f4f runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f2234063800 nid=0x2f50 runnable

"VM Periodic Task Thread" os_prio=0 tid=0x00007f22341ed800 nid=0x2f59 waiting on condition

JNI global references: 1925

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f2234061800 nid=0x2f4f runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f2234063800 nid=0x2f50 runnable

"VM Periodic Task Thread" os_prio=0 tid=0x00007f22341ed800 nid=0x2f59 waiting on condition

JNI global references: 1925

[root@iZwz9gs2zseivevv1k5vnkZ syp]# jstack 12107 | grep 2f50 -A 30
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f2234063800 nid=0x2f50 runnable

"VM Periodic Task Thread" os_prio=0 tid=0x00007f22341ed800 nid=0x2f59 waiting on condition

JNI global references: 1925

参考:

https://blog.csdn.net/moranzi1/article/details/89341480

原文地址:https://www.cnblogs.com/yucongblog/p/11987462.html