1.Java程序

import java.io.Serializable;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        while (true){ //一直循环创建User对象
            User user = new User();
            user.userName1 = "username1";
            user.userName2 = "userName2";
            user.userName3 = "userName3";
            user.userName4 = "userName4";
            user.userName5 = "userName5";
            user.userName6 = "userName6";
            user.userName7 = "userName7";
            user.userName8 = "userName8";
        }

    }

    public static void service(){
        return;
    }

    public static class User implements Serializable {
        public String userName1;
        public String userName2;
        public String userName3;
        public String userName4;
        public String userName5;
        public String userName6;
        public String userName7;
        public String userName8;
        public String userName9;
        public String userName10;
        public String userName11;
        public String userName12;
        public String userName13;
        public String userName14;
        public String userName15;
        public String userName16;
    }
}

2.top命令查看cpu占用率

top - 09:56:17 up 1 day,  9:53,  7 users,  load average: 1.34, 0.95, 0.77
Tasks: 139 total,   2 running, 137 sleeping,   0 stopped,   0 zombie
%Cpu(s): 51.9 us,  3.8 sy,  0.0 ni, 43.6 id,  0.0 wa,  0.0 hi,  0.7 si,  0.0 st
KiB Mem :  3880320 total,   137000 free,  3496672 used,   246648 buff/cache
KiB Swap:  2097148 total,  2076660 free,    20488 used.   159388 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
17679 root      20   0 3665040  32140  15900 S  91.0  0.8   0:18.84 java
15862 root      20   0  155032   5624   3936 S   0.7  0.1   0:30.12 sshd
24986 root      20   0 4607680 998.2m  12640 S   0.7 26.3  18:24.92 java
23783 root      20   0  151708   6408   1112 S   0.3  0.2   6:36.31 redis-sentinel
23790 root      20   0  149448   4840   1108 R   0.3  0.1   6:39.38 redis-sentinel
25350 root      20   0 4567896 973220  11788 S   0.3 25.1  12:45.53 java
25716 root      20   0 4574928 994456  11788 S   0.3 25.6  12:47.27 java
28449 root      20   0  155160   5644   3936 S   0.3  0.1   0:22.72 sshd
28610 root      20   0  162832   3032   1572 S   0.3  0.1   0:11.98 top
30011 root      20   0  155148   5628   3936 S   0.3  0.1   0:13.78 sshd
30158 root      20   0  162832   3024   1556 S   0.3  0.1   0:06.20 top
    1 root      20   0  128104   4568   2736 S   0.0  0.1   0:04.42 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.04 kthreadd
    4 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
    6 root      20   0       0      0      0 S   0.0  0.0   0:09.35 ksoftirqd/0

3.查看进程对应的线程占用cpu

top -p 23191 -H

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
23192 root      20   0 3665040  30560  15772 R 91.3  0.8   2:06.06 java
23191 root      20   0 3665040  30560  15772 S  0.0  0.8   0:00.00 java
23193 root      20   0 3665040  30560  15772 S  0.0  0.8   0:00.01 GC Thread#0
23194 root      20   0 3665040  30560  15772 S  0.0  0.8   0:00.00 G1 Main Marker
23195 root      20   0 3665040  30560  15772 S  0.0  0.8   0:00.00 G1 Conc#0
23196 root      20   0 3665040  30560  15772 S  0.0  0.8   0:00.00 G1 Refine#0
23197 root      20   0 3665040  30560  15772 S  0.0  0.8   0:00.01 G1 Young RemSet

4.获取23191线程堆栈信息

jstack 23191 > 23191.log,找到对应的高cpu占用线程23192(0x5d54)线程的状态。可以通过堆栈信息看到是Test.java中的问题

"main" #1 prio=5 os_prio=0 cpu=265491.21ms elapsed=290.57s tid=0x00007f5830014800 nid=0x5d54 runnable  [0x00007f58384a3000]
java.lang.Thread.State: RUNNABLE
     at Test.main(Test.java:15)

5.上述可以通过top + jstack进行查找高cpu线程和其堆栈信息

也可以通过脚本来实现查询高cpu占用线程

wget --no-check-certificate https://github.com/oldratlee/useful-scripts/archive/release-2.x.zip

unzip release-2.x.zip

github上的开源脚本
直接执行
/data/java/useful-scripts-release-2.x/bin中的

show-busy-java-threads脚本即可查看,默认显示前5名线程

结果

[1] Busy(90.4%) thread(14366/0x381e) stack of java process(14365) under user(root):
"main" #1 prio=5 os_prio=0 cpu=16787.88ms elapsed=18.53s tid=0x00007f4964014800 nid=0x381e runnable  [0x00007f496dc9f000]
   java.lang.Thread.State: RUNNABLE
        at Test.main(Test.java:15)

[2] Busy(0.0%) thread(14365/0x381d) stack of java process(14365) under user(root):

[3] Busy(0.0%) thread(24040/0x5de8) stack of java process(24040) under user(root):

[4] Busy(0.0%) thread(24043/0x5deb) stack of java process(24040) under user(root):
"main" #1 prio=5 os_prio=0 cpu=787.79ms elapsed=121813.53s tid=0x00007f9e9c015000 nid=0x5deb in Object.wait()  [0x00007f9ea6296000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait([email protected]/Native Method)
        - waiting on <0x00000000c631f8a0> (a org.apache.zookeeper.server.quorum.QuorumPeer)
        at java.lang.Thread.join([email protected]/Thread.java:1305)
        - waiting to re-lock in wait() <0x00000000c631f8a0> (a org.apache.zookeeper.server.quorum.QuorumPeer)
        at java.lang.Thread.join([email protected]/Thread.java:1379)
        at org.apache.zookeeper.server.quorum.QuorumPeerMain.runFromConfig(QuorumPeerMain.java:171)
        at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:114)
        at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:81)

[5] Busy(0.0%) thread(24098/0x5e22) stack of java process(24098) under user(root):
03-05 22:29