深入浅出 Arthas:Java 应用诊断神器实战指南
·
目录
七、类和类加载器 (sc/sm/jad/mc/redefine/dump/classloader)
摘要
Arthas 是阿里开源的一款强大的 Java 诊断工具,可以在不重启应用的情况下,实时查看 JVM 状态、监控方法调用、诊断性能瓶颈、反编译代码等。本文将结合详尽的大纲和丰富的代码实例,带你全面掌握 Arthas 的核心功能与实战技巧。
一、简介
- 1.1 核心功能概述
- 查看 JVM 信息(线程、内存、类加载器)。
- 监控方法执行耗时、调用次数、成功率。
- 查看方法调用的参数、返回值、异常。
- 反编译已加载的类源码。
- 动态修改运行时代码(需谨慎)。
- 生成性能分析火焰图。
- 1.2 相关资源
- GitHub: https://github.com/alibaba/arthas
- 官方文档: https://arthas.aliyun.com/doc/
二、安装 Arthas
# 下载最新版本 (以 3.6.7 为例)
curl -O https://arthas.aliyun.com/arthas-boot.jar
# 启动 Arthas,选择目标 Java 进程
java -jar arthas-boot.jar
三、核心监视功能
- 3.1 monitor:监控方法统计信息
monitor -c 60 com.example.service.UserService queryUser
输出示例:timestamp class method total success fail avg-ms(ms) min-ms(ms) max-ms(ms) 2023-10-26 14:30:00 com.example.service.UserService queryUser 120 118 2 25.3 10 150 - 3.2 watch:观察方法入参、返回值
watch com.example.service.UserService queryUser "{params, returnObj}" -x 3
输出示例:method=com.example.service.UserService.queryUser location=AtExit ... [omitted stack trace] ... params[0]=12345 returnObj=User{id=12345, name='arthas'} - 3.3 trace:追踪方法调用链路(定位耗时路径)
trace com.example.controller.UserController getUser '#cost > 1' -n 5
输出示例:---ts=2023-10-26 14:35:00;thread_name=http-nio-8080-exec-1;id=1e;is_daemon=true;priority=5;TCCL=AppClassLoader... `---[12.345678ms] com.example.controller.UserController:getUser() +---[0.123456ms] com.example.controller.UserController:parseRequest() #22 +---[10.234567ms] com.example.service.UserService:queryUser() #23 | `---[10.000000ms] com.example.dao.UserMapper:selectById() `---[1.987654ms] com.example.controller.UserController:buildResponse() #24 - 3.4 stack:查看方法调用堆栈
stack com.example.dao.UserMapper selectById
输出示例:... [omitted stack trace] ... at com.example.service.UserService.queryUser(UserService.java:25) at com.example.controller.UserController.getUser(UserController.java:18) ... [more callers] ... - 3.5 tt:方法调用时空隧道(记录、回放)
tt -t com.example.controller.UserController getUser -n 5
输出示例:INDEX TIMESTAMP COST(ms) IS-RET IS-EXP OBJECT CLASS METHOD 1000 2023-10-26 14:40:00 15.23 true false 0x12345678 UserController getUser- 记录特定条件的调用:
tt -t com.example.Foo doSomething "params[0].length > 0" - 回放指定调用:
tt -i 1000 -p
- 记录特定条件的调用:
四、项目中使用(实战技巧)
- 4.1 trace:定位耗时方法 (同核心功能中的 trace)
- 4.2 jad:反编译耗时代码 反编译
com.example.service.SlowService类,查看其源码:jad com.example.service.SlowService
输出示例:// Source code of com.example.service.SlowService package com.example.service; public class SlowService { public void doHeavyWork() { // ... decompiled code showing potentially slow operations ... } } - 4.3 watch:分析慢方法输入输出 (同核心功能中的 watch)
五、基础命令
helphelp watch# 查看 watch 命令的详细帮助cat /path/to/config.propertiesthread | grep httppwdsessionreset com.example.Fooreset *# 重置所有增强versionhistoryquitstopkeymap
六、JVM 相关命令
dashboardthreadthread 1thread -n 3# 显示最忙的前 3 个线程jvmsyspropsysprop user.dirsysprop myapp.debug truesysenvsysenv PATHvmtool --action getSystemProperties# 类似 syspropvmtool --action getCommandLinegetstatic com.example.Config MAX_SIZEognl '@java.lang.System@currentTimeMillis()'ognl '@com.example.Config@DEFAULT_TIMEOUT'
七、类和类加载器 (sc/sm/jad/mc/redefine/dump/classloader)
sc -d com.example.service.UserServicesm com.example.service.UserServicesm -d com.example.service.UserService queryUserdump com.example.service.UserService -d /tmpclassloaderclassloader -a --loader-name 'AppClassLoader'classloader -c 12345678# 使用 sc 查看到的 classLoaderHash
八、option 全局选项
查看当前所有全局选项:
option
设置选项示例:
option unsafe true # 允许某些 unsafe 操作 (需谨慎)
option json-format true # 输出 JSON 格式
option verbose false # 关闭详细输出
九、profiler 火焰图
Arthas 集成了 async-profiler,可以生成 CPU 或内存分配的火焰图。
- 9.1 开始采样 CPU (持续 30 秒):
profiler start --event cpu --duration 30 - 9.2 查看采样状态:
profiler status - 9.3 停止采样并生成火焰图 HTML 文件:
profiler stop --format html
(默认保存到arthas-output/,用浏览器打开生成的.html文件即可查看)
十、相关资源
- 官方文档: https://arthas.aliyun.com/doc/ - 最权威,最详细。
- GitHub 仓库: https://github.com/alibaba/arthas - 源码、Issue、Release。
- Awesome Arthas: https://github.com/eryajf/awesome-arthas - 社区整理的资源集合。
- Arthas Idea Plugin: 方便在 IDEA 中直接启动 Arthas。
总结
Arthas 功能强大,是 Java 开发者、运维人员诊断线上问题的必备工具。通过本文详细的功能介绍和代码实例,相信你已经掌握了 Arthas 的核心用法。建议在实际工作中多加练习,结合 help 命令探索更多高级功能,将其融入你的日常开发和问题排查流程中,提升效率和问题定位能力。
更多推荐

所有评论(0)