centos7-java模拟cpu占用高及排查

环境

centos7 1核2GB

Java8

模拟cpu占用高

新建一个名为jvm-learn的springboot项目

模拟代码如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class JvmLearnApplication {

    public static void main(String[] args) {
        SpringApplication.run(JvmLearnApplication.class, args);
    }

    @GetMapping("cpu")
    public void cpu() {
        while (true) {

        }
    }

}

打包

在项目根目录下执行如下命令:

mvn clean package -Dmaven.test.skip=true

启动jar包

nohup java -jar jvm-learn-0.0.1-SNAPSHOT.jar &

 

访问

curl localhost:8080/cpu

排查

查看占用cpu高的进程 

top

如下图:可看出PID为7149的java进程占用cpu最高,达到了98%

 查看进程中最耗cpu的子线程

top -p 7149 -H

如下图:可看出PID为7166的线程占用cpu最高,达到了97.7%

将最耗cpu的线程id转换为16进制输出

 printf "%x 
" 7166

 

 查询具体出现问题的代码位置

jstack 7149 | grep 1bfe -A 30

 如下图:可看出是JVMLearnApplication类的第18行出现问题

原文地址:https://www.cnblogs.com/zuidongfeng/p/10019262.html