arthas排查jvm中CPU占用过高问题解决
作者:笑松小站 时间:2022-07-15 20:31:49
安装 小试
记一次使用arthas
排查jvm中CPU占用过高问题。这工具 * 爆了 碾压我目前使用的全部JVM工具。
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar --repo-mirror aliyun --use-http
jar后面的参数也可以不加 加上只是为了下载速度更快
接下来arthas
控制台中显示了当前机器上jvm进程列表 输入需要排查的jvm进程号即可进入监控命令模式
找出CPU的元凶
处理问题之前 先想想如何去找到问题的原因 这个是解决问题个人觉得最重要的一步。
当前的现状是jvm启动后 cpu直接飙升到80+%
。而内存是正常的,可以认为大概率是某个线程占用了计算资源 导致的。所以第一步需要先把占用过高线程给揪出来。
这次使用arthas
排查。也顺便提一下以前记录过用top -Hp
的方法找出占用资源的线程PID 方法 top -Hp方法参考 。
输入命令 thread
查看所有线程信息 默认是按照cpu资源占用排名的
可以看到当前线程lettuce-nioEventLoop-4-1
占用cpu高达47.75。其实这个线程名称已经能定位到具体某个方向的问题了,所以线程名称的定义需要有意义 为了方便排查问题。
可以看出因为我们程序使用了lettuce
做redis的客户端,主要是使用了redis stream
StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, ObjectRecord<String, String>> containerOptions =
StreamMessageListenerContainer.StreamMessageListenerContainerOptions.builder()
.batchSize(10) // 一次性最多拉取多少条消息
.targetType(String.class) // 目标类型。统一使用 String
.executor(mqConsumerExecutor)
.pollTimeout(Duration.ZERO)//0不超时
.build();
把.pollTimeout(Duration.ZERO)
这一句改为 .pollTimeout(Duration.ofMillis(10))
cpu就正常了。
原因就是设置了永不超时 资源得不到释放。
改为指定时间超时后 程序一点问题都没有了。
查看线程栈的参数
可以直接使用thread pid
上图占用最高的id为22 则输入 thread 22
能看到类似jstack
的功能
"lettuce-nioEventLoop-4-1" Id=22 RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:101)
还有一个更好用的命令 -n
参数能显示top-n-threads 比上面一种更详细
"lettuce-nioEventLoop-4-1" Id=22 cpuUsage=49.51% deltaTime=99ms time=392976ms RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:101)
可以看出使用arthas
排除这类问题 比使用top -Hp
方便太多。当然这只是它的一个小功能而已。
来源:https://peachyy.gitee.io/2022/08/31/jvm_cpu_arthas_check/
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java全面细致讲解==和equals的使用
Android Framework如何实现Binder
关于java数组与字符串相互转换的问题
Android Studio实现标题栏和状态栏的隐藏
Java 基于tcp协议实现文件上传
C#将html table 导出成excel实例
浅析C# 9.0 新特性之 Lambda 弃元参数
![](https://img.aspxhome.com/file/2023/9/119569_0s.png)
java实现word文档转pdf并添加水印的方法详解
![](https://img.aspxhome.com/file/2023/9/62139_0s.png)
Android编程之Sdcard相关代码集锦
Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法
C#中改变DataGridView控件边框颜色的方法
一文详解Jetpack Android新一代导航管理Navigation
![](https://img.aspxhome.com/file/2023/1/139651_0s.jpg)
Java CompletableFuture 异步超时实现深入研究
java实现图片反色处理示例
![](https://img.aspxhome.com/file/2023/6/110926_0s.jpg)
聊一聊SpringBoot服务监控机制
![](https://img.aspxhome.com/file/2023/9/64169_0s.png)
详解C#中Helper类的使用
![](https://img.aspxhome.com/file/2023/7/108687_0s.png)
详解Android获取系统内核版本的方法与实现代码
c#使用Unity粒子实现炮塔发射系统
![](https://img.aspxhome.com/file/2023/2/92922_0s.png)
Java创建ZIP压缩文件的方法
C#中缓存的基本使用方法
![](https://img.aspxhome.com/file/2023/1/81141_0s.gif)