一 点睛
1 monitor 方法执行监控,调用次数、执行时间、失败率
-c 统计周期,默认值为120秒
2 watch 方法执行观测,能观察到的范围为:返回值、抛出异常、入参,通过编写 groovy 表达式进行对应变量的查看
-b 在方法调用之前观察(默认关闭)
-e 在方法异常之后观察(默认关闭)
-s 在方法返回之后观察(默认关闭)
-f 在方法结束之后(正常返回和异常返回)观察(默认开启)
-x 指定输岀结果的属性遍历深度,默认为0
3 trace 方法内部调用路径,并输出方法路径上的每个节点上耗时
-n 执行次数限制
4 stack 输出当前方法被调用的调用路径
5 tt 方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测
二 代码
package chapter03;
import java.util.ArrayList;
import java.util.Random;
/**
* -Xms600m -Xmx600m -XX:SurvivorRatio=8
* 老年代:400m
* 伊甸园:160m
* s0:20m
* s1:20m
*/
public class OOMTest {
public static void main(String[] args) {
ArrayList<Picture> list = new ArrayList<>();
while (true) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
list.add(new Picture(new Random().nextInt(100 * 50)));
}
}
}
class Picture {
private byte[] pixels;
public Picture(int length) {
this.pixels = new byte[length];
}
}
三 windows下进入命令界面
D:\ProgramFiles\arthas-packaging-3.5.4-bin>jps
10656 Jps
12736 Launcher
2864
10668 OOMTest
D:\ProgramFiles\arthas-packaging-3.5.4-bin>as.bat 10668
环境变量 JAVA_TOOL_OPTIONS 没有定义
JAVA_HOME: D:\ProgramFiles\Java\jdk1.8.0_251
telnet port: 3658
http port: 8563
信息: 用提供的模式无法找到文件。
telnet wasn't found, please google how to install telnet under windows.
Try to visit http://127.0.0.1:8563 to connecto arthas server.
四 实战
# monitor:每隔5秒打印一次方法执行情况
[arthas@16852]$ monitor -c 5 chapter03.Picture <init>
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 98 ms, listenerId: 3
timestamp class method total success fail avg-rt(ms) fail-rate
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2021-09-04 14:00:24 chapter03.Picture <init> 77 77 0 0.05 0.00%
timestamp class method total success fail avg-rt(ms) fail-rate
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2021-09-04 14:00:29 chapter03.Picture <init> 79 79 0 0.02 0.00%
timestamp class method total success fail avg-rt(ms) fail-rate
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2021-09-04 14:00:34 chapter03.Picture
# watch 方法执行观测,能观察到的范围为:返回值、抛出异常、入参
[arthas@16852]$ watch chapter03.Picture <init>
method=chapter03.Picture.<init> location=AtExit
ts=2021-09-04 14:03:18; [cost=0.0176ms] result=@ArrayList[
@Object[][isEmpty=false;size=1],
@Picture[chapter03.Picture@4d76f3f8],
null,
]
method=chapter03.Picture.<init> location=AtExit
ts=2021-09-04 14:03:18; [cost=0.0184ms] result=@ArrayList[
@Object[][isEmpty=false;size=1],
@Picture[chapter03.Picture@2d8e6db6],
null,
]
# 关注返回参数和结果 -x:返回深度 这里为2
[arthas@16852]$ watch chapter03.Picture <init> "{params,returnObj}" -x 2
method=chapter03.Picture.<init> location=AtExit
ts=2021-09-04 14:06:50; [cost=0.0157ms] result=@ArrayList[
@Object[][
@Integer[2930],
],
null,
]
method=chapter03.Picture.<init> location=AtExit
ts=2021-09-04 14:06:50; [cost=0.0202ms] result=@ArrayList[
@Object[][
@Integer[2113],
],
null,
]
Command execution times exceed limit: 100, so command will exit. You can set it with -n option.
# 默认深度为1
[arthas@16852]$ watch chapter03.Picture <init> "{params,returnObj}"
method=chapter03.Picture.<init> location=AtExit
ts=2021-09-04 14:09:43; [cost=0.0197ms] result=@ArrayList[
@Object[][isEmpty=false;size=1],
null,
]
method=chapter03.Picture.<init> location=AtExit
ts=2021-09-04 14:09:43; [cost=0.0298ms] result=@ArrayList[
@Object[][isEmpty=false;size=1],
null,
]
Command execution times exceed limit: 100, so command will exit. You can set it with -n option.
# trace 方法内部调用路径,并输出方法路径上的每个节点上耗时
[arthas@16852]$ trace chapter03.Picture <init>
`---ts=2021-09-04 14:11:50;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@18b4aac2
`---[0.0125ms] chapter03.Picture:<init>()
`---ts=2021-09-04 14:11:50;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@18b4aac2
`---[0.0146ms] chapter03.Picture:<init>()
# -n:限定执行次数,这里是 5次
[arthas@16852]$ trace -n 5 chapter03.Picture <init>
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 38 ms, listenerId: 8
`---ts=2021-09-04 14:15:41;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@18b4aac2
`---[0.1016ms] chapter03.Picture:<init>()
`---ts=2021-09-04 14:15:41;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@18b4aac2
`---[0.0088ms] chapter03.Picture:<init>()
`---ts=2021-09-04 14:15:42;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@18b4aac2
`---[0.0057ms] chapter03.Picture:<init>()
`---ts=2021-09-04 14:15:42;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@18b4aac2
`---[0.0131ms] chapter03.Picture:<init>()
`---ts=2021-09-04 14:15:42;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@18b4aac2
`---[0.0076ms] chapter03.Picture:<init>()
Command execution times exceed limit: 5, so command will exit. You can set it with -n option.
# stack 输出当前方法被调用的调用路径
[arthas@16852]$ stack chapter03.Picture <init>
ts=2021-09-04 14:17:12;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@18b4aac2
@chapter03.Picture.<init>()
at chapter03.OOMTest.main(OOMTest.java:22)
ts=2021-09-04 14:17:12;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@18b4aac2
@chapter03.Picture.<init>()
at chapter03.OOMTest.main(OOMTest.java:22)
# tt 方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测
[arthas@16852]$ tt -t -n 3 chapter03.Picture <init>
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 36 ms, listenerId: 10
INDEX TIMESTAMP COST(ms) IS-RET IS-EXP OBJECT CLASS METHOD
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1000 2021-09-04 14:20:24 0.1697 true false 0x7506e922 Picture <init>
1001 2021-09-04 14:20:24 0.0126 true false 0x573fd745 Picture <init>
1002 2021-09-04 14:20:24 0.02 true false 0x15327b79 Picture <init>
Command execution times exceed limit: 3, so command will exit. You can set it with -n option.