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

记一次使用 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 方便太多。当然这只是它的一个小功能而已。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值