当前位置:网站首页>面试拆解:系统上线后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使用率飙升后如何排查这个问题不仅面试中经常会问,而且在实际工作中也非常有用,大家最好根据上述步骤实际操作一下,这样才能记得住记得牢。
本文出自知识星球面试拆解系列,欢迎大家一起来玩~

边栏推荐
- Web knowledge supplement
- remount of the / superblock failed: Permission denied
- Iptables foundation and Samba configuration examples
- 7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
- .Net之延迟队列
- Don't turn down, three sentences to clarify the origin of cross domain resource request errors
- Haproxy high availability solution
- Talk about the design and implementation logic of payment process
- WS2811 M是三通道LED驱动控制专用电路彩灯带方案开发
- Etcd storage, watch and expiration mechanism
猜你喜欢

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

面试官:Redis中哈希数据类型的内部实现方式是什么?

Haproxy high availability solution

OpenHarmony应用开发之如何创建DAYU200预览器

逆向调试入门-PE结构-资源表07/07

一次 Keepalived 高可用的事故,让我重学了一遍它

三星量产3纳米产品引台媒关注:能否短期提高投入产出率是与台积电竞争关键

字节面试算法题

HAProxy高可用解决方案

Etcd storage, watch and expiration mechanism
随机推荐
C foundation in-depth learning II
Alibaba cloud award winning experience: build a highly available system with polardb-x
ASP.NET Core入门一
C语言集合运算
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
Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout
C language dormitory management query software
免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy
AI painting minimalist tutorial
Reading cognitive Awakening
Comprehensive evaluation of modular note taking software: craft, notation, flowus
.NET 使用 redis
Web knowledge supplement
How real-time cloud interaction helps the development of education industry
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
SQL statement syntax error in test SQL statement deletion in eclipse linked database
ASP. Net core introduction I
MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
逆向调试入门-PE结构-资源表07/07
模块化笔记软件综合评测:Craft、Notion、FlowUs