安装,
官网下载对应系统的jar包
下载 | arthas (aliyun.com)
下载demo源码 https://arthas.aliyun.com/math-game.jar
启动demojava -jar math-game.jar运行 java -jar arthas-boot.jar
PS C:\Users\1> java -jar .\arthas-boot.jar
[INFO] JAVA_HOME: D:\Program Files\openjdk-11
[INFO] arthas-boot version: 3.7.1
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 13268
[2]: 1956 org.jetbrains.jps.cmdline.Launcher
[3]: 5784 .\math-game.jar
选择具体的应用进程,输入序号即可, 我这里是 序号3
dashboard命令,查看进行情况
# dashboard -h 可以看到命令说明
[arthas@4108]$ dashboard -i 5000
ID NAME GROUP PRIORITY STATE %CPU DELTA_TIM TIME INTERRUPT DAEMON
1 main main 5 RUNNABLE 0.0 0.000 2:47.390 false false
-1 C2 CompilerThread0 - -1 - 0.0 0.000 0:0.734 false true
-1 C1 CompilerThread0 - -1 - 0.0 0.000 0:0.234 false true
47 arthas-NettyWebsocketTtyBoots system 5 RUNNABLE 0.0 0.000 0:0.171 false true
34 arthas-NettyHttpTelnetBootstr system 5 RUNNABLE 0.0 0.000 0:0.062 false true
5 Attach Listener system 5 RUNNABLE 0.0 0.000 0:0.046 false true
36 arthas-NettyWebsocketTtyBoots system 5 RUNNABLE 0.0 0.000 0:0.046 false true
-1 VM Thread - -1 - 0.0 0.000 0:0.031 false true
28 arthas-NettyWebsocketTtyBoots system 5 RUNNABLE 0.0 0.000 0:0.015 false true
32 arthas-UserStat system 5 WAITING 0.0 0.000 0:0.015 false true
35 arthas-NettyWebsocketTtyBoots system 5 RUNNABLE 0.0 0.000 0:0.015 false true
Memory used total max usage GC
heap 55M 252M 4022M 1.38% gc.g1_young_generation.count 3
g1_eden_space 28M 154M -1 18.18% gc.g1_young_generation.time(m 14
g1_old_gen 22M 93M 4022M 0.56% s)
g1_survivor_space 5M 5M -1 100.00% gc.g1_old_generation.count 0
nonheap 43M 47M -1 91.86% gc.g1_old_generation.time(ms) 0
codeheap_'non-nmethods' 1M 2M 7M 17.28%
metaspace 31M 32M -1 98.48%
Runtime
os.name Windows 10
os.version 10.0
java.version 11.0.19
java.home D:\Program Files\openjdk-11
systemload.average -1.00
processors 16
timestamp/uptime Fri Nov 03 14:06:29 GMT+08:00 2023/167s
- 查看线程1的主类, 类名为 MathGame
[arthas@5784]$ thread 1 | grep 'main('
at app//demo.MathGame.main(MathGame.java:17)
- 查看已加载类 sc
[arthas@5784]$ sc -d *MathGame
class-info demo.MathGame
code-source /C:/Users/1/Downloads/math-game.jar
name demo.MathGame
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name MathGame
modifier public
annotation
interfaces
super-class +-java.lang.Object
class-loader +-jdk.internal.loader.ClassLoaders$AppClassLoader@2328c243
+-jdk.internal.loader.ClassLoaders$PlatformClassLoader@64736d2f
classLoaderHash 2328c243
Affect(row-cnt:1) cost in 15 ms.
- 反编译 jad
[arthas@5784]$ jad --source-only demo.MathGame
/*
* Decompiled with CFR.
*/
package demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class MathGame {
private static Random random = new Random();
private int illegalArgumentCount = 0;
public List<Integer> primeFactors(int number) {
/*44*/ if (number < 2) {
/*45*/ ++this.illegalArgumentCount;
throw new IllegalArgumentException("number is: " + number + ", need >= 2");
}
ArrayList<Integer> result = new ArrayList<Integer>();
/*50*/ int i = 2;
/*51*/ while (i <= number) {
/*52*/ if (number % i == 0) {
/*53*/ result.add(i);
/*54*/ number /= i;
/*55*/ i = 2;
continue;
}
/*57*/ ++i;
}
/*61*/ return result;
}
public static void main(String[] args) throws InterruptedException {
MathGame game = new MathGame();
while (true) {
/*16*/ game.run();
/*17*/ TimeUnit.SECONDS.sleep(1L);
}
}
public void run() throws InterruptedException {
try {
/*23*/ int number = random.nextInt() / 10000;
/*24*/ List<Integer> primeFactors = this.primeFactors(number);
/*25*/ MathGame.print(number, primeFactors);
}
catch (Exception e) {
/*28*/ System.out.println(String.format("illegalArgumentCount:%3d, ", this.illegalArgumentCount) + e.getMessage());
}
}
public static void print(int number, List<Integer> primeFactors) {
StringBuffer sb = new StringBuffer(number + "=");
/*34*/ for (int factor : primeFactors) {
/*35*/ sb.append(factor).append('*');
}
/*37*/ if (sb.charAt(sb.length() - 1) == '*') {
/*38*/ sb.deleteCharAt(sb.length() - 1);
}
/*40*/ System.out.println(sb);
}
}
- watch可以查看函数的参数/返回值/异常信息
[arthas@5784]$ watch demo.MathGame primeFactors "{params,returnObj}"
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 112 ms, listenerId: 1
method=demo.MathGame.primeFactors location=AtExit
ts=2023-11-03 15:22:58; [cost=4.2593ms] result=@ArrayList[
@Integer[2],
@Integer[48563],
]
method=demo.MathGame.primeFactors location=AtExit
ts=2023-11-03 15:22:59; [cost=3.1675ms] result=@ArrayList[
@Integer[2],
@Integer[2],
@Integer[45667],
]
- vmtool获取内存中对象
vmtool --action getInstances --className demo.MathGame --limit 10
- 调用实例的方法
vmtool --action getInstances --className demo.MathGame --express 'instances[0].primeFactors(3)' -x 3
- 打断某个线程
vmtool --action interruptThread -t 线程id
- 获取类的静态属性
getstatic 类 变量名称
[arthas@2432]$ getstatic com.example.javastudydemo.demos.day14.Demo2 xyz
field: xyz
@Integer[10]
Affect(row-cnt:1) cost in 3 ms.
- ognl 获取变量值
[arthas@2432]$ ognl @com.example.javastudydemo.demos.day14.Demo2@xyz
@Integer[10]
# dump 到指定文件
heapdump arthas-output/dump.hprof
# 下载文件
http://localhost:8563/arthas-output/
- 根据状态展示线程
[arthas@4248]$ thread --state WAITING
Threads Total: 16, NEW: 0, RUNNABLE: 10, BLOCKED: 0, WAITING: 3, TIMED_WAITING: 3, TERMINATED: 0
ID NAME GROUP PRIORITY STATE %CPU DELTA_TIM TIME INTERRUPT DAEMON
3 Finalizer system 8 WAITING 0.0 0.000 0:0.000 false true
24 arthas-timer system 5 WAITING 0.0 0.000 0:0.000 false true
32 arthas-UserStat system 5 WAITING 0.0 0.000 0:0.000 false true
- 分析cpu执行情况
profiler start -d 15 -f /tmp/1.txt
- 通过trace命令,查看调用过程
- 使用watch命令,查看问题
- sc 查看jvm加载的类
[arthas@24901]$ sc -d *MathGame
class-info demo.MathGame
code-source /root/math-game.jar
name demo.MathGame
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name MathGame
modifier public
annotation
interfaces
super-class +-java.lang.Object
class-loader +-jdk.internal.loader.ClassLoaders$AppClassLoader@46fbb2c1
+-jdk.internal.loader.ClassLoaders$PlatformClassLoader@31cbb4ec
classLoaderHash 46fbb2c1
Affect(row-cnt:1) cost in 88 ms.