Skip to content

Arthas 安装/基础指令

Arthas:线上问题诊断神器

Arthas 是阿里巴巴开源的 Java 诊断工具,类似于 BTrace,但更强大、更易用。它不需要重启 JVM,不需要修改代码,就能对运行中的 Java 程序进行诊断和热修复。

为什么需要 Arthas

传统诊断方式的痛点:
✗ 问题定位需要反复重启
✗ 添加日志需要重新部署
✗ 生产环境无法 attach
✗ 无法实时观察运行时状态

Arthas 的优势:
✓ 无侵入:不需要改代码,不需要重启
✓ 功能丰富:监控、方法调用、热修复一站式
✓ 交互友好:命令行界面,配色清晰
✓ 社区活跃:阿里开源,持续维护

安装 Arthas

方式一:一行命令安装(推荐)

bash
# 下载 arthas-boot.jar 并启动
curl -L https://arthas.aliyun.com/arthas-boot.jar -o arthas-boot.jar
java -jar arthas-boot.jar

# 或直接执行(自动下载)
java -jar arthas-boot.jar

方式二:手动安装

bash
# 下载 Arthas 最新版本
wget https://github.com/alibaba/arthas/releases/latest/download/arthas-bin.zip
unzip arthas-bin.zip
cd arthas

# 启动
./bin/arthas.sh

方式三:IDEA 插件

IntelliJ IDEA:
  File → Settings → Plugins → 搜索 "Arthas"
  → 安装 Arthas IDEA 插件
  → 可以直接生成 Arthas 命令

Docker 方式

bash
# 运行 Arthas Docker 容器
docker run -it --rm \
  --net=host \
  -v /tmp/arthas:/tmp/arthas \
  registry.cn-hangzhou.aliyuncs.com/arthas/arthas:latest

# 连接到指定 JVM
docker exec -it <container_id> arthas-demo <pid>

启动与连接

启动 Arthas

bash
# 基本启动
java -jar arthas-boot.jar

# 指定要 attach 的进程
java -jar arthas-boot.jar <pid>

# 远程连接
java -jar arthas-boot.jar *<host>:8888

Arthas 启动后的交互界面

Arthas 启动后显示:

  JAVA_HOME: /usr/lib/jvm/java-17
  arthas home: /home/user/.arthas
  bootstrap jar: /home/user/.arthas/arthas-boot.jar
  Stats wall time: 8m
  Session timeout: 30m

  Found existing java process, please choose one and input the serial number:

  [1]: 12345 com.example.MyApplication
  [2]: 67890 org.apache.catalina.startup.Bootstrap

  select the process:

# 输入数字选择要诊断的进程

连接成功后

连接成功后显示:

  ,---.  ,------.  ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  |  |  | /  O  \ '   .-'
|  .-.  || '--.'.'  |  |   |  |  |  ||  .-.  |`.  `-.
|  | |  ||  |\  \   |  |   |  '--'  ||  | |  |.-'    |
|  '--'  /|  | '.  |  |  ||  |  |  | '--'  / '-----'
`-----'` |  `-'  `-'  `--'`-'  '--'  '---'   `-----'

wiki      https://arthas.aliyun.com/doc
tutorial  https://arthas.aliyun.com/doc/arthas-tutorials.html
contact   https://gitter.im/alibaba/arthas

[arthas@12345]$ _

退出 Arthas

bash
# 退出当前连接(保持 JVM 连接)
quit
# 或
q

# 完全停止 Arthas
stop

常用基础指令

1. dashboard:实时监控面板

bash
# 查看系统实时面板
dashboard

# 面板内容:
# - Java 进程信息(PID、启动时间)
# - 线程信息(运行中、阻塞、等待)
# - 内存信息(堆各区域使用量)
# - GC 信息(GC 次数、耗时)
# - Runtime 信息(系统属性)
# - 按 Ctrl+C 退出
dashboard 输出示例:

 Dashboard
 runtime
 os: Linux 5.4.0 amd64; java version 17.0.2
 java.version: 17.0.2
 uid: a2b3c4d5
 process: 12345 @ localhost
 process uptime: 2h 30m
 cpu: 12.3%
 total started threads: 45
 daemon threads: 38
 peak threads: 52
 memory
 heap: 1.5GB / 2.0GB  used
 non-heap: 256MB / 512MB  used
 gc
 gc count: 12 times  cost 1.23s
 g1gc
  gcc: 12
  gct: 1.234s
 threads
  name: http-nio-8080-exec-1  tid: 0x12345  state: WAITING
  name: http-nio-8080-exec-2  tid: 0x12346  state: RUNNABLE
 ...

2. sysprop:系统属性

bash
# 查看所有系统属性
sysprop

# 查看特定属性
sysprop java.version

# 设置系统属性(可动态生效)
sysprop user.timezone Asia/Shanghai

3. sysenv:环境变量

bash
# 查看所有环境变量
sysenv

# 查看特定环境变量
sysenv JAVA_HOME
sysenv PATH

4. jvm:JVM 信息

bash
# 查看 JVM 信息
jvm

# 输出包括:
# - CLASS: 类加载信息
# - MEMORY: 内存池信息
# - GC: GC 收集器信息
# - THREAD: 线程信息
# - FILE: 文件描述符
# - SOCKET: Socket 连接

5. 基础帮助

bash
# 查看所有可用命令
help

# 查看特定命令帮助
help dashboard
help thread
help jad

线程相关指令

6. thread:线程分析

bash
# 查看所有线程
thread

# 查看指定线程
thread 1

# 查看最忙的 N 个线程
thread -n 5

# 查看阻塞的线程
thread -b

# 查看线程堆栈
thread 12345

# 按 CPU 使用率排序
thread -n 3 --search-running

7. jstack:线程堆栈

bash
# 生成线程堆栈
jstack

# 相当于 JVM 的 jstack
jstack 12345

类和方法相关指令

8. sc:搜索类

bash
# 模糊搜索类
sc -d com.example.*

# 搜索包含特定字符串的类
sc *User*

# 查看类的详细信息(包括 ClassLoader)
sc -d com.example.User

# 输出示例:
#  class-info        com.example.User
#  class-loader      sun.misc.Launcher$AppClassLoader@7c2d1a3c
#  classLoader       0x7f8a12345678
#  super-class       java.lang.Object
#  interfaces        java.io.Serializable
#  fields           name:Ljava/lang/String;age:I

9. sm:搜索方法

bash
# 查看类的所有方法
sm -d com.example.User

# 搜索包含特定名称的方法
sm java.lang.Thread *

# 查看方法的详细信息
sm -d java.lang.Thread sleep

监控和观察指令

10. monitor:方法调用监控

bash
# 监控方法调用(默认 120 秒)
monitor -c 5 com.example.UserService getUser

# 参数说明:
# -c 5 → 统计周期(默认 120 秒)
# -n 100 → 最多统计 100 次
# --use-regexp → 使用正则表达式

# 输出示例:
#  timestamp         class        method   total  success  fail  avg-rt(ms)  fail-rate
#  2026-03-22 10:00  UserService  getUser   120     115     5     12.34       4.17%

11. watch:观察方法执行

bash
# 查看方法入参和返回值
watch com.example.UserService getUser '{params, returnObj}'

# 查看方法执行前后的对象
watch com.example.UserService getUser '{params[0].name, returnObj}' -x 2

# 条件过滤:只观察参数等于特定值的情况
watch com.example.UserService getUser '{params, returnObj}' 'params[0]=="admin"'

# 查看异常
watch com.example.UserService getUser '{params, throwExp}' -e

# -x 2 → 展开深度(默认 1)
# -e → 只看异常

12. trace:方法内部调用路径

bash
# 追踪方法内部调用路径
trace com.example.UserService getUser

# 超过指定时间才显示
trace com.example.UserService getUser '#cost > 10'

# 同时追踪多个类
trace com.example.UserService com.example.OrderService *

# 输出示例:
#  `---[10ms] com.example.UserService:getUser()
#      `---[5ms] com.example.UserDAO:findById()
#          `---[3ms] java.sql.Connection:prepareStatement()

13. stack:查看方法调用栈

bash
# 查看方法被调用的堆栈
stack com.example.UserService getUser

# 条件过滤
stack com.example.UserService getUser '#params[0]=="admin"'

14. tt:方法执行记录(Time Tunnel)

bash
# 记录方法每次执行的入参和返回值
tt -t com.example.UserService getUser

# 查看历史记录
tt -l

# 查看指定记录的详情
tt -i 1000

# 重新执行历史调用
tt -p -i 1000

本节小结

Arthas 基础指令速查:

指令作用常用参数
dashboard实时监控面板-
sysprop系统属性- 查看所有
sysenv环境变量- 查看所有
jvmJVM 信息-
thread线程分析-n 5 最忙线程,-b 阻塞
sc搜索类-d 详细信息
sm搜索方法-d 详细信息
monitor方法调用监控-c 统计周期
watch观察方法执行{params, returnObj} 表达式
trace追踪调用路径#cost > 10 条件
stack查看调用堆栈条件过滤
tt方法执行记录-t 录制,-p 重放

Arthas 是生产环境诊断的第一选择——无侵入、实时、功能全面,下一节我们来看更高级的 dashboard/thread/heapdump 命令

基于 VitePress 构建