来源:https://alibaba.github.io/arthas/install-detail.html
下载与安装
wget https://alibaba.github.io/arthas/arthas-boot.jar java -jar arthas-boot.jar
启动
查看启动的帮助信息:
java -jar arthas-boot.jar -h
一个小例子
下载一个demo并启动这个demo项目
wget https://alibaba.github.io/arthas/arthas-demo.jar java -jar arthas-demo.jar
再启动"arthas"
java -jar arthas-boot.jar
接着输入"1",表示你想要查看"arthas-demo"
此时,进入了"arthas"查看"arthas-demo"的控制台
输入"dashboard"
每5s会更新信息
"Ctrl+C"退出dashboard
通过thread命令来获取到arthas-demo进程的Main Class
thread 1
thread 1 | grep 'main('
通过jad来反编译Main Class
jad 包名.类名
$ jad demo.MathGame ClassLoader: +-sun.misc.Launcher$AppClassLoader@3d4eac69 +-sun.misc.Launcher$ExtClassLoader@66350f69 Location: /tmp/arthas-demo.jar /* * Decompiled with CFR 0_132. */ package demo; import java.io.PrintStream; import java.util.ArrayList; import java.util.Iterator; 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 static void main(String[] args) throws InterruptedException { MathGame game = new MathGame(); do { game.run(); TimeUnit.SECONDS.sleep(1L); } while (true); } public void run() throws InterruptedException { try { int number = random.nextInt(); List<Integer> primeFactors = this.primeFactors(number); MathGame.print(number, primeFactors); } catch (Exception e) { 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 + "="); Iterator<Integer> iterator = primeFactors.iterator(); while (iterator.hasNext()) { int factor = iterator.next(); sb.append(factor).append('*'); } if (sb.charAt(sb.length() - 1) == '*') { sb.deleteCharAt(sb.length() - 1); } System.out.println(sb); } public List<Integer> primeFactors(int number) { if (number < 2) { ++this.illegalArgumentCount; throw new IllegalArgumentException("number is: " + number + ", need >= 2"); } ArrayList<Integer> result = new ArrayList<Integer>(); int i = 2; while (i <= number) { if (number % i == 0) { result.add(i); number /= i; i = 2; continue; } ++i; } return result; } } Affect(row-cnt:1) cost in 970 ms.
watch
watch 包名.类名 方法名 returnObj
多使用"tab"键,会自动补全。
通过watch命令来查看demo.MathGame#primeFactors函数的返回值:
更多的功能可以查看进阶使用。
Sc
可以通过 sc
命令来查找JVM里已加载的类:
退出arthas
如果只是退出当前的连接,可以用quit或者exit命令。Attach到目标进程上的arthas还会继续运行,端口会保持开放,下次连接时可以直接连接上。
如果想完全退出arthas,可以执行shutdown命令。