Arthas 排查JVM问题总结

一、安装

在Arthas官网:https://arthas.aliyun.com/中下载安装包。

执行java -jar arthas-boot.jar就可以启动。

image-20230908101838783

二、常见命令

  • dashboard:查看JVM全局概览,包括线程、堆内存、GC还有系统信息等

image-20230908004057353

  • thread:常见命令,查看线程。通过thread 查看线程详情信息。

image-20230908004335178

通过thread -b查看阻塞线程信息。

image-20230908004426789

  • jad:反编译命令,能够将class文件反编译回源码。可以用在生产环境比较版本是否更新。

image-20230908004631929

  • watch:查看方法参数

  • trace:查看方法内部调用路径,可以看到每条路径的耗时

  • stack:查看方法调用路径

  • redefine:将编译好的class热部署到环境

  • ognl:可以查看类中属性和方法执行情况。

image-20230908005400528

三、实践

示例代码

public class Arthas {

    private static HashSet hashSet = new HashSet();

    public String getName() {
        return "arthas";
    }

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

    /**
     * 不断的向 hashSet 集合添加数据
     */
    public static void addHashSetThread() {
        // 初始化常量
        new Thread(() -> {
            int count = 0;
            while (true) {
                try {
                    hashSet.add("count" + count);
                    Thread.sleep(1000);
                    count++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"my-arthas-thread").start();
    }

    public static void cpuHigh() {
        new Thread(() -> {
            while (true) {

            }
        },"my-arthas-thread1").start();
    }

    /**
     * 死锁
     */
    private static void deadThread() {
        /** 创建资源 */
        Object resourceA = new Object();
        Object resourceB = new Object();
        // 创建线程
        Thread threadA = new Thread(() -> {
            synchronized (resourceA) {
                System.out.println(Thread.currentThread() + " get ResourceA");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "waiting get resourceB");
                synchronized (resourceB) {
                    System.out.println(Thread.currentThread() + " get resourceB");
                }
            }
        },"my-arthas-thread2");

        Thread threadB = new Thread(() -> {
            synchronized (resourceB) {
                System.out.println(Thread.currentThread() + " get ResourceB");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "waiting get resourceA");
                synchronized (resourceA) {
                    System.out.println(Thread.currentThread() + " get resourceA");
                }
            }
        },"my-arthas-thread3");
        threadA.start();
        threadB.start();
    }
}

查看死锁

通过thread -b查看死锁情况:

image-20230908104407779

查看CPU飙升

通过dashboard就能查看CPU占用情况:

image-20230908104527155

查看GC情况

通过dashboard就能查看GC情况:

image-20230908104659145

查看接口调用太慢

通过stack 来查看方法调用次数和时长。

image-20230908105101387

四、总结

E4B52353-A4D1-413A-A98F-68658D32F749

参考资料

  1. Arthas官网:https://arthas.aliyun.com/
  2. Arthas 使用详解:https://blog.csdn.net/zhangcongyi420/article/details/127252866
  3. 5-4-问题排查:https://www.pdai.tech/md/interview/x-interview.html#_5-4-问题排查

    本文由博客一文多发平台 OpenWrite 发布!

Arthas是一个非常方便的Java应用性能诊断工具,可以帮助我们监控和分析JVM线程。 Arthas通过所谓的"剖析"对JVM线程进行监控。"剖析"是指在运行时对目标应用程序进行代码注入和操作,从而获取应用程序的运行信息。Arthas使用了Java的Instrumentation API,以字节码增强的方式,动态修改目标类的代码,使之能够收集和输出线程相关的信息。 使用Arthas监控JVM线程非常简单。首先,我们需要在目标Java应用程序的启动命令行中加入Arthas的Agent参数,例如: java -jar arthas-boot.jar 然后,我们可以使用命令"thread"来监控JVM线程。例如,我们可以使用"thread"命令来查看当前所有线程的堆栈信息,包括线程ID、线程名称、线程状态以及调用栈信息。 另外,Arthas还提供了一些其他的命令来更细粒度地监控线程。比如:"thread -n 5"可以指定仅显示前5个线程的堆栈信息,"thread -t 1"可以指定只显示处于RUNNABLE状态的线程。 此外,Arthas还提供了丰富的过滤条件,用于更准确地筛选要监控的线程。比如,我们可以使用"thread -n 5 -p xxx"来查看线程名称中含有"xxx"的前5个线程的信息。 总而言之,通过Arthas,我们可以方便地监控JVM线程,了解线程的状态和调用栈信息,帮助我们进行应用程序的性能分析和故障排查。使用Arthas,我们可以及时发现线程问题,并采取相应的措施来优化我们的Java应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangnk42

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值