当前位置:网站首页>面试拆解:系统上线后Cpu使用率飙升如何排查?
面试拆解:系统上线后Cpu使用率飙升如何排查?
2022-07-04 12:48:00 【飘渺Jam】
大家好,我是飘渺。
上次面试官问了个问题:应用上线后Cpu使用率飙升如何排查?
其实这是个很常见的问题,也非常简单,那既然如此我为什么还要写呢?因为上次回答的时候我忘记将线程PID转换成16进制的命令了。
所以我决定再重温一遍这个问题,当然贴心的我还给大家准备好了测试代码,大家可以实际操作一下,这样下次就不会忘记了。
模拟一个高CPU场景
public class HighCpuTest {
public static void main(String[] args) {
List<HignCpu> cpus = new ArrayList<>();
Thread highCpuThread = new Thread(()->{
int i = 0;
while (true){
HignCpu cpu = new HignCpu("Java日知录",i);
cpus.add(cpu);
System.out.println("high cpu size:" + cpus.size());
i ++;
}
});
highCpuThread.setName("HignCpu");
highCpuThread.start();
}
}在main方法中开启了一个线程,无限构建HighCpu对象。
@Data
@AllArgsConstructor
public class HignCpu {
private String name;
private int age;
}准备好上面的代码,运行HighCpuTest,然后就可以开始一些列的操作来发现问题原因了。
排查步骤
第一步,使用 top 找到占用 CPU 最高的 Java 进程
1. 监控cpu运行状,显示进程运行信息列表
top -c
2. 按CPU使用率排序,键入大写的P
P
第二步,用 top -Hp 命令查看占用 CPU 最高的线程
上一步用 top命令找到了那个 Java 进程。那一个进程中有那么多线程,不可能所有线程都一直占着 CPU 不放,这一步要做的就是揪出这个罪魁祸首,当然有可能不止一个。
执行top -Hp pid命令,pid 就是前面的 Java 进程,我这个例子中就是 16738 ,完整命令为:
top -Hp 16738,然后键入P (大写p),线程按照CPU使用率排序
执行之后的效果如下

查到占用CPU最高的那个线程 PID 为 16756
第三步,查看堆栈信息,定位对应代码
通过printf命令将其转化成16进制,之所以需要转化为16进制,是因为堆栈里,线程id是用16进制表示的。(我当时就是忘记这个命令了~)
[[email protected] ~]# printf "%x\n" 16756
4174得到16进制的线程ID为4174。
通过jstack命令查看堆栈信息
jstack 16738 | grep '0x4174' -C10 --color
如上图,找到了耗CPU高的线程对应的线程名称“HighCpu”,以及看到了该线程正在执行代码的堆栈。
最后,根据堆栈里的信息,定位到对应死循环代码,搞定。
小结
cpu使用率飙升后如何排查这个问题不仅面试中经常会问,而且在实际工作中也非常有用,大家最好根据上述步骤实际操作一下,这样才能记得住记得牢。
本文出自知识星球面试拆解系列,欢迎大家一起来玩~

边栏推荐
- ASP.NET Core入门一
- Is the outdoor LED screen waterproof?
- 舔狗舔到最后一无所有(状态机)
- Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
- N++ is not reliable
- Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务
- 聊聊支付流程的设计与实现逻辑
- 用fail2ban阻止密码尝试攻
- Iptables foundation and Samba configuration examples
- Fs4056 800mA charging IC domestic fast charging power IC
猜你喜欢

It is six orders of magnitude faster than the quantum chemical method. An adiabatic artificial neural network method based on adiabatic state can accelerate the simulation of dual nitrogen benzene der

Samsung's mass production of 3nm products has attracted the attention of Taiwan media: whether it can improve the input-output rate in the short term is the key to compete with TSMC

2022G3锅炉水处理考试题模拟考试题库及模拟考试

C#/VB. Net to add text / image watermarks to PDF documents

Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务

高质量软件架构的唯一核心指标

Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle

ViewBinding和DataBinding的理解和区别

Haproxy high availability solution

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
随机推荐
Distributed base theory
WS2811 M是三通道LED驱动控制专用电路彩灯带方案开发
C语言小型商品管理系统
.Net之延迟队列
"Tips" to slim down Seurat objects
C语言职工管理系统
C#基础深入学习一
Web knowledge supplement
CTF competition problem solution STM32 reverse introduction
C#基础补充
Three schemes to improve the efficiency of MySQL deep paging query
Scrapy 框架学习
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
C foundation in-depth study I
Using scrcpy projection
N++ is not reliable
Annual comprehensive analysis of China's mobile reading market in 2022
C language dormitory management query software
Node mongodb installation
OPPO Find N2产品形态首曝:补齐各项短板