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%
相关推荐
阿尔的代码屋5 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿1 天前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780512 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng82 天前
Python+Django+H5+MySQL项目搭建
python·django