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

边栏推荐
- C语言个人通讯录管理系统
- Using nsproxy to forward messages
- Etcd storage, watch and expiration mechanism
- 7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
- Oracle 被 Ventana Research 评为数字创新奖总冠军
- C#基础深入学习一
- XML入门一
- Service Mesh的基本模式
- Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
- 高效!用虚拟用户搭建FTP工作环境
猜你喜欢

Oracle 被 Ventana Research 评为数字创新奖总冠军

Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)

A data person understands and deepens the domain model

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

CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer

.Net之延迟队列

Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)

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

Oracle was named the champion of Digital Innovation Award by Ventana research

Dgraph: large scale dynamic graph dataset
随机推荐
Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
Besides, rsync+inotify realizes real-time backup of data
高质量软件架构的唯一核心指标
Reading cognitive Awakening
光环效应——谁说头上有光的就算英雄
remount of the / superblock failed: Permission denied
Rsyslog配置及使用教程
Zhongang Mining: in order to ensure sufficient supply of fluorite, it is imperative to open source and save flow
7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
Database lock table? Don't panic, this article teaches you how to solve it
JVM系列——栈与堆、方法区day1-2
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
在 Apache 上配置 WebDAV 服务器
2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance
室外LED屏幕防水吗?
用fail2ban阻止密码尝试攻
C#基础深入学习二
Introduction to XML II
基于链表管理的单片机轮询程序框架