编程语言
首页 > 编程语言> > java的CPU使用达到100%咋排查

java的CPU使用达到100%咋排查

作者:互联网

java的CPU使用达到100%咋排查

面试的时候好像问过两次,没有一次答得比较靠谱,今天就对着网上的博客,自己手动操作一遍,并记录一下。

javaCPU 100%的原因

1: 死循环
2:CPU密集型计算

死循环的java代码

public class TestCPU100 {  
    static class MyThread extends Thread {  
        public void run() { 
            int i = 0;  
            while (true) {  
                i++;  
            }  
        }  
    }  
    public static void main(String args[]) throws InterruptedException {  
        new MyThread().start();  
        Thread.sleep(1000*60);  
    }  
} 

javac java

[root@localhost home]# javac TestCPU100.java
[root@localhost home]# ll
total 12
-rw-r--r--  1 root root 494 Feb 27 12:48 TestCPU100.class
-rw-r--r--  1 root root 380 Feb 27 12:47 TestCPU100.java
-rw-r--r--  1 root root 345 Feb 27 12:48 TestCPU100$MyThread.class
drwx------. 2 tom  tom   62 Apr 11  2018 tom
[root@localhost home]#
[root@localhost home]#
[root@localhost home]#
[root@localhost home]#
[root@localhost home]#
[root@localhost home]#
[root@localhost home]#
[root@localhost home]#
[root@localhost home]# java TestCPU100

top 查进程

可以看到11826 root 20 0 6726996 22968 10272 S 100.3 0.1 0:16.97 java
100%的java进程是pid=11826

[root@localhost ~]# top
top - 12:48:28 up 6 days, 21:16,  8 users,  load average: 0.22, 0.06, 0.06
Tasks: 188 total,   1 running, 184 sleeping,   3 stopped,   0 zombie
%Cpu(s): 25.3 us,  0.3 sy,  0.0 ni, 73.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.6 st
KiB Mem : 16425988 total,  6402548 free,  7889164 used,  2134276 buff/cache
KiB Swap:  6160380 total,  6160380 free,        0 used.  8340448 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
11826 root      20   0 6726996  22968  10272 S 100.3  0.1   0:16.97 java
 1763 root      20   0 8038880   1.1g  15328 S   0.7  7.1  43:50.02 java
 1901 root      20   0 8038728 987.4m  15272 S   0.7  6.2  43:33.38 java
23650 root      20   0 7855940   1.5g  13732 S   0.7  9.5  33:14.01 java
11929 root      20   0  162016   2296   1536 R   0.3  0.0   0:00.05 top
23482 root      20   0       0      0      0 S   0.3  0.0   0:44.09 kworker/0:2
24375 root      20   0 7961052   1.1g  14040 S   0.3  6.8   7:30.25 java
    1 root      20   0  125696   4112   2500 S   0.0  0.0   0:04.83 systemd
................
[root@localhost ~]#

top 查线程

可以看到是11841 root 20 0 6793560 27204 10372 R 99.9 0.2 1:52.16 java 线程11841 的CPU是99%

[root@localhost ~]# top -p 11826 -H
top - 12:50:03 up 6 days, 21:18,  8 users,  load average: 0.98, 0.40, 0.18
Threads:  17 total,   1 running,  16 sleeping,   0 stopped,   0 zombie
%Cpu0  :  2.0 us,  2.0 sy,  0.0 ni, 95.9 id,  0.0 wa,  0.0 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  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  2.0 us,  2.0 sy,  0.0 ni, 94.0 id,  0.0 wa,  0.0 hi,  0.0 si,  2.0 st
KiB Mem : 16425988 total,  6396032 free,  7892984 used,  2136972 buff/cache
KiB Swap:  6160380 total,  6160380 free,        0 used.  8336440 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
11841 root      20   0 6793560  27204  10372 R 99.9  0.2   1:52.16 java
11826 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11827 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.05 java
11828 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11829 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11830 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11831 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11832 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11833 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11834 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11835 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11836 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11837 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11838 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11839 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java
11840 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.05 java
12182 root      20   0 6793560  27204  10372 S  0.0  0.2   0:00.00 java

线程ID对应的十六进制

[root@localhost ~]# printf "%x\n" 11841
2e41

jstack 查看线程

[root@localhost ~]# jstack 11826
2021-02-27 12:48:59
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.281-b09 mixed mode):

"Attach Listener" #10 daemon prio=9 os_prio=0 tid=0x00007ff5b8001000 nid=0x2f96 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Thread-0" #9 prio=5 os_prio=0 tid=0x00007ff6000ee000 nid=0x2e41 runnable [0x00007ff5ed46c000]
   java.lang.Thread.State: RUNNABLE
        at TestCPU100$MyThread.run(TestCPU100.java:6)

"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007ff6000ca800 nid=0x2e3f runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007ff6000bf800 nid=0x2e3e waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007ff6000bd800 nid=0x2e3d waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007ff6000ba800 nid=0x2e3c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007ff6000b9000 nid=0x2e3b runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007ff600086000 nid=0x2e3a in Object.wait() [0x00007ff5edb73000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076c708ee0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
        - locked <0x000000076c708ee0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007ff600081800 nid=0x2e39 in Object.wait() [0x00007ff5edc74000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076c706c00> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
        - locked <0x000000076c706c00> (a java.lang.ref.Reference$Lock)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"main" #1 prio=5 os_prio=0 tid=0x00007ff600009800 nid=0x2e33 waiting on condition [0x00007ff606e01000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at TestCPU100.main(TestCPU100.java:12)

"VM Thread" os_prio=0 tid=0x00007ff600078000 nid=0x2e38 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007ff60001e800 nid=0x2e34 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007ff600020800 nid=0x2e35 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007ff600022000 nid=0x2e36 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007ff600024000 nid=0x2e37 runnable

"VM Periodic Task Thread" os_prio=0 tid=0x00007ff6000cd800 nid=0x2e40 waiting on condition

JNI global references: 5

可以看到


"Thread-0" #9 prio=5 os_prio=0 tid=0x00007ff6000ee000 nid=0x2e41 runnable [0x00007ff5ed46c000]
   java.lang.Thread.State: RUNNABLE
        at TestCPU100$MyThread.run(TestCPU100.java:6)

prio os_prio tid nid 啥意思

  1. prio java线程的优先级
  2. os_prio os线程的优先级
  3. tid java线程的id
  4. nid os线程的id

标签:lang,java,prio,0.0,100%,20,root,CPU
来源: https://blog.csdn.net/xiaolixi199311/article/details/114170284