阿里巴巴Arthas详解

Arthas Alibaba 在 2018 年 9 月开源的 Java 诊断工具。支持 JDK6+, 采用命令行交互模式,可以方便的定位和诊断线上程序运行问题。Arthas 官方文档十分详细,详见:https://alibaba.github.io/arthas

Arthas使用

  1. # github下载arthas
  2. wget https://alibaba.github.io/arthas/arthasboot.jar
  3. # 或者 Gitee 下载
  4. wget https://arthas.gitee.io/arthasboot.jar

用java -jar运行即可,可以识别机器上所有Java进程(我们这里之前已经运行了一个Arthas测试程序,代码见下方)

1 package com.tuling.jvm;

2

3 import java.util.HashSet;

4

5 public class Arthas {

6

7 private static HashSet hashSet = new HashSet();

8

  1. public static void main(String[] args) {
  2. // 模拟 CPU 过高
  3. cpuHigh();
  4. // 模拟线程死锁
  5. deadThread();
  6. // 不断的向 hashSet 集合增加数据
  7. addHashSetThread();

16 }

17

18 /**

19 * 不断的向 hashSet 集合添加数据

20 */

  1. public static void addHashSetThread() {
  2. // 初始化常量
  3. new Thread(() ‐> {
  4. int count = 0;
  5. while (true) {
  6. try {
  7. hashSet.add("count" + count);
  8. Thread.sleep(1000);
  9. count++;
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();

32 }

33 }

34 }).start();

35 }

36

  1. public static void cpuHigh() {
  2. new Thread(() ‐> {
  3. while (true) {

40

41 }

42 }).start();

43 }

44

45 /**

46 * 死 锁

47 */

48 private static void deadThread() {

49 /** 创建资源 */

  1. Object resourceA = new Object();
  2. Object resourceB = new Object();
  3. // 创建线程
  4. Thread threadA = new Thread(() ‐> {
  5. synchronized (resourceA) {
  6. System.out.println(Thread.currentThread() + " get ResourceA");
  7. try {
  8. Thread.sleep(1000);
  9. } catch (InterruptedException e) {
  10. e.printStackTrace();

60 }

  1. System.out.println(Thread.currentThread() + "waiting get resourceB");
  2. synchronized (resourceB) {
  3. System.out.println(Thread.currentThread() + " get resourceB");

64 }

65 }

66 });

67

  1. Thread threadB = new Thread(() ‐> {
  2. synchronized (resourceB) {
  3. System.out.println(Thread.currentThread() + " get ResourceB");
  4. try {
  5. Thread.sleep(1000);
  6. } catch (InterruptedException e) {
  7. e.printStackTrace();

75 }

  1. System.out.println(Thread.currentThread() + "waiting get resourceA");
  2. synchronized (resourceA) {
  3. System.out.println(Thread.currentThread() + " get resourceA");

79 }

80 }

81 });

  1. threadA.start();
  2. threadB.start();

84 }

85 }

选择进程序号1,进入进程信息操作

输入dashboard可以查看整个进程的运行情况,线程、内存、GC、运行环境信息:

输入thread可以查看线程详细情况

输入 thread加上线程ID 可以查看线程堆栈

输入 thread -b 可以查看线程死锁

输入 jad加类的全名 可以反编译,这样可以方便我们查看线上代码是否是正确的版本

使用 ognl 命令可以查看线上系统变量的值,甚至可以修改变量的值

更多命令使用可以用help命令查看,或查看文档:https://alibaba.github.io/arthas/commands.html#arthas

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章