记一次用arthas排查jvm中CPU占用过高问题

2022-11-06,,,,

记一次使用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/

公众号

记一次用arthas排查jvm中CPU占用过高问题的相关教程结束。

《记一次用arthas排查jvm中CPU占用过高问题.doc》

下载本文的Word格式文档,以方便收藏与打印。