官方文档:https://arthas.aliyun.com/doc
1. 下载arthas
curl -O https://arthas.aliyun.com/arthas-boot.jar
2. 启动
直接用java -jar的方式启动:
java -jar arthas-boot.jar --repo-mirror aliyun --use-http
说明:由于arthas监控的是jvm的进程,因此,启动arthas之前需要提前启动jvm的进程
先运行咱们的测试类Java04
java Java04
演示CPU飙高代码
public class Java04 {
public static void main(String[] args) {
new Thread(()->{
while (true){
System.out.println("1111111");
}
},"录单-thread").start();
}
}
3. 选择指定jvm进程
然后,arthas就会提示,已经找到服务器上的关于jvm的进行列表,请选择进行编号,然后arthas就会对你指定的jvm进程进行监控,并将监控日志输出到目录中。
top -c
4. 筛选线程
查询当前进行中,哪个线程占用CPU比较高呢
排列出当前进程前3个占用cpu较高的线程
thread -n 3
5. 日志分析
[arthas@26289]$ thread -n 3
"录单-thread" Id=8 cpuUsage=92.79% deltaTime=189ms time=52158ms RUNNABLE (in native)
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at java.io.PrintStream.write(PrintStream.java:482)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
at java.io.PrintStream.newLine(PrintStream.java:546)
at java.io.PrintStream.println(PrintStream.java:807)
at Java04.lambda$main$0(Java04.java:6)
at Java04$$Lambda$1/471910020.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"arthas-command-execute" Id=23 cpuUsage=1.06% deltaTime=2ms time=10ms RUNNABLE
at sun.management.ThreadImpl.dumpThreads0(Native Method)
at sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:448)
"VM Periodic Task Thread" [Internal] cpuUsage=0.25% deltaTime=0ms time=58ms
[arthas@26289]$
从日志可以分析出:
①"录单-thread" Id=8 cpuUsage=92.79% ,是在Java04类中导致的,进一步查看这个类中“录单-thread”的处理业务
②"arthas-command-execute" Id=23 cpuUsage=1.06%,这个是arthas监控工具。