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

边栏推荐
- Apache server access log access Log settings
- 担心“断气” 德国正修改《能源安全法》
- C语言职工管理系统
- MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍
- XML入门一
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
- Node mongodb installation
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
- 免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy
猜你喜欢

CANN算子:利用迭代器高效实现Tensor数据切割分块处理

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

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

Etcd storage, watch and expiration mechanism

2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance

ASP. Net core introduction I

Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)

Animation and transition effects

Is the outdoor LED screen waterproof?

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
模块化笔记软件综合评测:Craft、Notion、FlowUs
Comprehensive evaluation of modular note taking software: craft, notation, flowus
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
2022G3锅炉水处理考试题模拟考试题库及模拟考试
Scripy framework learning
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
Efficient! Build FTP working environment with virtual users
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
js中的变量提升和函数提升
聊聊支付流程的设计与实现逻辑
Web knowledge supplement
Source code compilation and installation of MySQL
iptables基础及Samba配置举例
提高MySQL深分页查询效率的三种方案
Oracle 被 Ventana Research 评为数字创新奖总冠军
用fail2ban阻止密码尝试攻
Talk about the design and implementation logic of payment process
C语言程序设计
unity不识别rider的其中一种解决方法