Arthas 火焰图的使用

Arthas 是阿里巴巴开源的 Java 诊断工具,火焰图是其中一个重要功能,用于可视化程序性能分析。

主要特点

  • 可视化性能分析:直观展示代码执行热点和调用栈
  • 实时监控:可实时查看应用性能状况
  • CPU 分析:主要分析 CPU 使用情况和耗时分布

基本命令

1. 生成火焰图

bash 复制代码
profiler start
# 等待一段时间收集数据
profiler stop

2. 指定采样时间

bash 复制代码
profiler start --duration 60

这会采集 60 秒的数据后自动生成火焰图

3. 查看支持的事件类型

bash 复制代码
profiler list

4. 指定特定事件

bash 复制代码
profiler start -e cpu

输出格式

  • 生成的火焰图通常保存为 HTML 文件
  • 可以在浏览器中打开交互式查看
  • 支持缩放和平移操作

实际应用场景

  1. 性能瓶颈定位:快速发现耗时较多的方法
  2. 热点分析:找出频繁调用的方法
  3. 调用链分析:了解方法间的调用关系
  4. CPU 使用优化:识别高 CPU 占用代码

注意事项

  • 需要确保目标进程有足够的权限运行 profiler
  • 采样时间过短可能导致数据不准确
  • 在生产环境使用时要注意对性能的影响

结果解读

  • 横轴表示采样比例,宽度越大表示占用时间越长
  • 纵轴表示调用栈深度
  • 颜色通常是随机分配,不具特殊含义

火焰图是性能分析的强大工具,能够帮助开发人员快速定位性能问题。

https://github.com/async-profiler/async-profiler

使用示例

bash 复制代码
[op@Core-marketo-02 project]$ java -jar  arthas-boot.jar 
[INFO] JAVA_HOME: /project/server/jdk-17.0.2
[INFO] arthas-boot version: 4.0.5
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 2166 api-8.0.0.jar
  [2]: 1687 org.apache.zookeeper.server.quorum.QuorumPeerMain
  [3]: 151557 web-8.0.0.jar
  [4]: 2136 service-8.0.0.jar


[INFO] local latest version: 4.0.5, remote latest version: 4.1.5, try to download from remote.
[INFO] Start download arthas from remote server: https://arthas.aliyun.com/download/4.1.5?mirror=center
[INFO] Download arthas success.
[INFO] arthas home: /home/op/.arthas/lib/4.1.5/arthas
[INFO] Try to attach process 2166
Picked up JAVA_TOOL_OPTIONS: 
[INFO] Attach process 2166 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                           
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'                          
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.                          
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |                         
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'                          

wiki         https://arthas.aliyun.com/doc                                      
tutorials    https://arthas.aliyun.com/doc/arthas-tutorials.html                
version      4.1.5                                                              
main_class   api-8.0.0.jar   
pid          2166                                                               
start_time   2026-01-23 08:04:22.102                                            
current_time 2026-01-28 09:54:56.779                                            

[arthas@2166]$ profiler start
Profiling started
[arthas@2166]$ profiler list
Basic events:
  cpu
  alloc
  nativemem
  lock
  wall
  itimer
  ctimer
Java method calls:
  ClassName.methodName
[arthas@2166]$ profiler stop
OK
profiler output file: /project/20260128-095611.html
[arthas@2166]$ profiler start -e cpu
Profiling started
[arthas@2166]$ profiler stop
OK
profiler output file: /project/20260128-095646.html
[arthas@2166]$ exit
Session has been terminated.
Arthas is still running in the background.
To completely shutdown arthas, please execute the 'stop' command.
[op@Core-marketo-02 project]$ sz /project/20260128-095611.html

下载 20260128-095611.html: 15.0 KB 100%

OO[op@Core-marketo-02 project]sz /project/20260128-095646.html

下载 20260128-095646.html: 8.5 KB 100%
相关推荐
Lxinccode2 小时前
python(70) : 网页IDE
开发语言·ide·python·网页ide
zmjjdank1ng2 小时前
理解bash和shell
linux·运维·开发语言·bash
码界奇点2 小时前
基于Beego v2与Go语言的网站管理后台系统设计与实现
开发语言·golang·毕业设计·go语言·源代码管理·beego
m0_748233172 小时前
Laravel vs ThinkPHP:谁更适合你?
java·开发语言
Java后端的Ai之路2 小时前
【Python小知识】-pip install xxx 命令安装的 ,在电脑的哪个盘?
开发语言·python·pip·pip install
henujolly2 小时前
How do you troubleshoot a CI failure?
java·开发语言·ci/cd
Java后端的Ai之路2 小时前
【Python小知识】-Python Flask 框架入门教程
开发语言·python·flask
张张努力变强2 小时前
C++ 类和对象(五):初始化列表、static、友元、内部类等7大知识点全攻略
开发语言·数据结构·c++·算法
AI浩2 小时前
Python包离线下载
开发语言·人工智能·python·目标检测