jvm调优实战-阿里Arthas使用详解

       相关系列

JVM调优工具详解-jps、jmap、jstat、jstack-CSDN博客

jvm调优案例分析-分析oom问题-CSDN博客

jvm调优案例分析-分析oom问题-CSDN博客

jvm运行情况预估-CSDN博客

Arthas是什么?

        Arthas是阿里在2018年9月开源的Java诊断工具。支持JDK6+,采用交互模式。可以很方便用于线上定位问题。官方文档地址:https://alibaba.github.io/arthas

下载安装

下载

#通过github下载arthas
wget https://alibaba.github.io/arthas/arthas‐boot.jar
#或者通过Gitee下载
wget https://arthas.gitee.io/arthas‐boot.jar

使用arthas

执行指令java -jar ,arthas会自动识别出目前在机器运行的所有线程

测试案例代码

import java.util.HashSet;

public class ArthasDemo {

    private static HashSet hashSet = new HashSet();

    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);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * cpu使用过高
     */
    public static void cpuHigh(){
        new Thread(()->{
            int count =0;
            while(true){
                try{
                    hashSet.add("count" + count);
                    Thread.sleep(1000);
                    count ++;
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 死锁
     */
    public static void deadThread(){
        Object objA = new Object();
        Object objB = new Object();

        Thread threadA = new Thread(()->{
           synchronized (objA){
               System.out.println(Thread.currentThread() + " get ResurceA");
               try{
                   Thread.sleep(1000);
               }catch (InterruptedException e){
                   e.printStackTrace();
               }
               System.out.println(Thread.currentThread() +"waiting get resourceB");
               synchronized (objB){
                   System.out.println(Thread.currentThread() + " get resourceB");
               }
           }
        });

        Thread threadB = new Thread(()->{
            synchronized (objB){
                System.out.println(Thread.currentThread() + " get ResurceB");
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() +"waiting get resourceA");
                synchronized (objA){
                    System.out.println(Thread.currentThread() + " get resourceA");
                }
            }
        });

        threadA.start();
        threadB.start();
    }
}

如何使用arthas指令?

重新运行java -jar arthas-boot.jar

输入1,按回车

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

 输入thread

输入thead 加上线程ID 可以查看线程死锁 

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

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

使用ognl命令可以查看线上变量的值

 使用ognl命令修改变量的值

  • 12
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值