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 文件
- 可以在浏览器中打开交互式查看
- 支持缩放和平移操作
实际应用场景
- 性能瓶颈定位:快速发现耗时较多的方法
- 热点分析:找出频繁调用的方法
- 调用链分析:了解方法间的调用关系
- 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%