news 2026/6/23 9:51:34

三步搞定CPU飙升!Arthas实战指南:thread+trace+profiler高效排查法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
三步搞定CPU飙升!Arthas实战指南:thread+trace+profiler高效排查法

一、引言

在 Java 应用的高并发战场中,CPU 使用率突然飙升至 90%+ 是常见 “险情”。传统排查方式如重启服务、离线分析线程 dump,往往耗时费力且难以捕捉实时现场。 Arthas 作为阿里巴巴开源的 Java 诊断神器,支持在不重启应用的情况下,通过 thread 命令定位问题线程、trace 命令追踪方法耗时、profiler 命令生成火焰图,三步锁定 CPU 瓶颈,成为线上诊断的 “瑞士军刀”。本文将通过实战案例,带你掌握这套高效排查方案。

二、完整排查流程

使用thread+trace+profiler组合排查CPU高问题是Java应用性能诊断的黄金搭档。接下来我将详细介绍这个完整的排查流程:

三、第一阶段:快速定位问题线程

3.1 查看系统整体状态

# 1. 启动arthas并连接目标进程 java -jar arthas-boot.jar # 2. 查看实时仪表板(最常用) dashboard # 3. 查看CPU使用率最高的前5个线程 thread -n 5 # 4. 持续监控CPU变化(每3秒刷新) dashboard -i 3000

3.2 分析线程状态

# 1. 查看所有线程状态 thread # 2. 按CPU使用时间排序 thread --all # 3. 查找阻塞的线程 thread -b # 4. 查看特定状态的线程 thread --state RUNNABLE thread --state BLOCKED thread --state WAITING

3.3 定位具体问题线程

# 1. 找出最耗CPU的线程ID thread -n 3 -i 1000 # 2. 查看指定线程的详细堆栈 thread 12345 # 3. 批量查看多个线程 thread 12345 23456 # 4. 查看线程运行时间线 thread -t

四、第二阶段:深入分析热点方法

4.1 使用trace分析热点方法调用链

# 1. 根据线程堆栈找到热点方法,进行trace trace com.example.service.HotService hotMethod '#cost > 10' # 2. 统计方法调用次数和耗时 trace com.example.service.* * -n 1000 # 3. 查看方法内部每个调用的耗时 trace --skipJDKMethod false com.example.service.HotService hotMethod # 4. 只显示最耗时的部分 trace com.example.service.HotService hotMethod '#cost > 50' -n 20

4.2 结合线程信息进行针对性trace

# 1. 如果发现某个线程ID持续占用CPU # 先获取线程正在执行的方法 thread 12345 # 假设发现线程在执行 processData 方法 trace com.example.DataProcessor processData # 2. 监控特定线程执行的方法 # 启动后台任务监控 trace com.example.DataProcessor * '#cost > 20' -j 12345

五、第三阶段:使用profiler进行火焰图分析

5.1 基础profiler使用

# 1. 启动CPU性能采样 profiler start # 2. 采样30秒后停止 profiler stop --duration 30 # 3. 保存为html格式 profiler stop --format html --file /tmp/cpu_profile.html # 4. 查看profiler状态 profiler status

5.2 高级profiler功能

# 1. 指定采样事件 profiler start --event cpu # 2. 指定采样间隔(默认10ms) profiler start --interval 5ms # 3. 同时监控多种事件 profiler start --event cpu,alloc,lock # 4. 采样特定时间段 profiler start # 等待问题重现 profiler stop --file /tmp/cpu-hot.svg

5.3 自动化采样脚本

#!/bin/bash # 自动采样脚本 cpu_profile.sh PID=$1 DURATION=60 INTERVAL=5 echo "开始对进程 $PID 进行CPU分析,持续时间 ${DURATION}秒" java -jar arthas-boot.jar $PID << EOF # 启动profiler采样 profiler start --interval ${INTERVAL}ms # 等待指定时间 sleep ${DURATION} # 停止采样并保存 profiler stop --format svg --file /tmp/cpu_profile_$(date +%Y%m%d_%H%M%S).svg # 同时保存为html格式 profiler stop --format html --file /tmp/cpu_profile_$(date +%Y%m%d_%H%M%S).html quit EOF echo "分析完成,文件保存在 /tmp/"

六、实战案例:CPU持续100%问题排查

6.1 死循环问题

# 1. 查看CPU使用情况 dashboard # 2. 找出CPU使用率最高的线程 thread -n 3 # 输出示例: # Thread 36: CPU usage: 85.12% # com.example.service.ReportService.generateReport(ReportService.java:123) # java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:580) # 3. trace热点方法 trace com.example.service.ReportService generateReport # 4. 如果发现是循环问题,进一步分析 watch com.example.service.ReportService generateReport 'params[0].size()' -x 1 # 5. 使用profiler生成火焰图确认 profiler start --duration 30 profiler stop --file /tmp/deadloop.html

6.2 大量数据处理

# 1. 查找CPU高的线程 thread -n 5 # 2. 发现是数据处理线程 trace com.example.processor.DataProcessor processBatch # 3. 查看方法内部耗时分布 trace --skipJDKMethod false com.example.processor.DataProcessor processBatch '#cost > 5' # 4. 发现是JSON解析耗时,进一步分析 watch com.fasterxml.jackson.databind.ObjectMapper readValue '{params, #cost}' '#cost > 50' # 5. 使用profiler分析CPU热点 profiler start # 等待一段时间 profiler stop --format html --file /tmp/json_parse_profile.html

6.3 并发锁竞争

# 1. 查看线程状态,发现很多BLOCKED线程 thread --state BLOCKED # 2. 查找死锁 thread -b # 3. 监控锁竞争 profiler start --event lock # 4. 分析具体锁竞争的方法 trace java.util.concurrent.locks.ReentrantLock lock # 5. 查看持有锁的线程 thread --locked

6.4 综合脚本

#!/bin/bash # 完整的CPU问题诊断脚本 diagnose_cpu.sh PID=$1 THRESHOLD=80 # CPU阈值 echo "开始诊断进程 $PID 的CPU问题..." java -jar arthas-boot.jar $PID << EOF echo "=== 1. 系统整体状态 ===" dashboard echo "=== 2. CPU最高的5个线程 ===" thread -n 5 echo "=== 3. 线程状态统计 ===" echo "RUNNABLE线程:" thread --state RUNNABLE | head -10 echo "" echo "BLOCKED线程:" thread --state BLOCKED | head -10 echo "=== 4. 启动profiler采样(30秒)===" profiler start --interval 10ms echo "=== 5. 监控期间的热点方法追踪 ===" # 获取top 3线程的堆栈,并提取热点方法 thread -n 3 > /tmp/top_threads.txt # 这里可以解析top_threads.txt,自动trace热点方法 # 示例:假设发现 com.example.Service.process 是热点 trace com.example.Service process '#cost > 20' -n 50 & echo "=== 等待30秒采样... ===" sleep 30 echo "=== 6. 停止采样并保存结果 ===" profiler stop --format svg --file /tmp/cpu_analysis.svg profiler stop --format html --file /tmp/cpu_analysis.html echo "=== 7. 生成诊断报告 ===" # 保存当前线程状态 thread --all > /tmp/thread_dump_$(date +%H%M%S).txt echo "诊断完成!" quit EOF echo "分析结果:" echo "1. 火焰图: /tmp/cpu_analysis.html" echo "2. 线程堆栈: /tmp/thread_dump_*.txt" echo "3. Top线程信息: /tmp/top_threads.txt"

七、小结

面对 CPU 飙升问题,Arthas 的 thread、trace、profiler 组合拳提供了从 “线程定位→方法追踪→可视化分析” 的完整链路:

  1. thread:快速锁定高 CPU 线程,精准到具体代码行。

  2. trace:量化方法调用耗时,暴露算法或逻辑缺陷。

  3. profiler:火焰图可视化热点,让性能瓶颈一目了然。

掌握这套方法,无需重启应用、无需海量日志,即可在生产环境中高效诊断 CPU 问题,真正实现 “线上问题线上解决”。

Tips: 为了大家快速高效的学习,已经将文章提交到了git仓库,涵盖后端大部分技术,以及后端学习路线,仓库内容会持续更新,建议Star 收藏以便随时查看https://gitee.com/bxlj/java-article。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/23 0:44:21

服务器日志排查

后端开发来说&#xff0c;熟练掌握 Linux 的日志分析命令是基本功&#xff0c;整理几一些基于 tail、less、grep、sed、awk 的日志查询场景&#xff0c;希望能帮你快速定位问题。tail很多新手习惯用 cat&#xff0c;但对于大文件&#xff0c;cat 会导致屏幕刷屏&#xff0c;还容…

作者头像 李华
网站建设 2026/6/23 2:02:38

终极ThinkPad风扇控制:TPFanCtrl2完全配置指南

终极ThinkPad风扇控制&#xff1a;TPFanCtrl2完全配置指南 【免费下载链接】TPFanCtrl2 ThinkPad Fan Control 2 (Dual Fan) for Windows 10 and 11 项目地址: https://gitcode.com/gh_mirrors/tp/TPFanCtrl2 还在为ThinkPad风扇噪音困扰吗&#xff1f;TPFanCtrl2作为专…

作者头像 李华
网站建设 2026/6/23 18:11:22

如何彻底告别命令行:Applite图形化软件管理完全指南

如何彻底告别命令行&#xff1a;Applite图形化软件管理完全指南 【免费下载链接】Applite User-friendly GUI macOS application for Homebrew Casks 项目地址: https://gitcode.com/gh_mirrors/ap/Applite 还在为复杂的终端命令而头疼吗&#xff1f;macOS上的软件管理一…

作者头像 李华
网站建设 2026/6/22 18:18:45

工业控制硬件设计中AD原理图生成PCB的注意事项解析

工业控制硬件设计中AD原理图转PCB的实战要点全解析在工业自动化、智能制造和物联网快速发展的今天&#xff0c;控制系统对硬件稳定性和抗干扰能力的要求达到了前所未有的高度。作为连接电路构想与物理实现的关键环节&#xff0c;PCB设计不仅关乎功能能否落地&#xff0c;更直接…

作者头像 李华
网站建设 2026/6/23 1:17:09

MyTV-Android多线路播放源配置终极指南

还在为电视直播频繁卡顿而烦恼吗&#xff1f;&#x1f914; MyTV-Android播放器的多线路播放功能就是你的救星&#xff01;这个强大的功能让你为同一个电视频道配置多个播放源&#xff0c;从此告别播放中断的困扰。&#x1f389; 【免费下载链接】mytv-android 使用Android原生…

作者头像 李华
网站建设 2026/6/23 10:18:32

Google Drive仅查看PDF下载终极指南:2025最新解决方案

你是否曾经在Google Drive上找到一份重要的PDF文档&#xff0c;却因为"仅查看"权限而无法下载保存&#xff1f;这种情况在学术研究、工作资料整理时尤为常见。今天&#xff0c;我将为你带来一款简单实用的下载工具&#xff0c;帮助你轻松解决这一困扰。 【免费下载链…

作者头像 李华