Arthas 排查JVM问题总结

一、安装

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

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

二、常见命令

  • dashboard:查看JVM全局概览,包括线程、堆内存、GC还有系统信息等
  • thread:常见命令,查看线程。通过thread 查看线程详情信息。

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

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

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

  • stack:查看方法调用路径

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

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

三、实践

示例代码

复制代码
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查看死锁情况:

查看CPU飙升

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

查看GC情况

通过dashboard就能查看GC情况:

查看接口调用太慢

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

四、总结

参考资料

  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 发布!
相关推荐
超级小忍1 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
喝可乐的布偶猫6 小时前
Java类变量(静态变量)
java·开发语言·jvm
abigalexy8 小时前
深入JVM底层-垃圾回收GC算法
jvm
麦兜*1 天前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
真实的菜2 天前
JVM类加载系统详解:深入理解Java类的生命周期
java·开发语言·jvm
在未来等你2 天前
JVM调优实战 Day 15:云原生环境下的JVM配置
java·jvm·性能优化·虚拟机·调优
黄雪超2 天前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice2 天前
对象的finalization机制Test
java·开发语言·jvm
很小心的小新2 天前
12、jvm运行期优化
java·开发语言·jvm·笔记
ThetaarSofVenice2 天前
垃圾收集相关算法Test
java·jvm·算法