jvm常用监控指标(记笔记)

最近项目里面要做组件监控,于是整理了一下jvm常用的监控指标记录下来,废话不多讲,直接上代码

类名说明
JvmRuntimeInfo
jvm运行时的指标统计实体类
JvmMonitor
jvm运行时的指标统计执行类
Builder
通用构建对象的Builder
public class JvmRuntimeInfo {
    //组件名称
    private String componentName = "suc-data-dispatcher";
    //总内存
    private String totalMemory;
    //可用内存
    private String freeMemory;
    //已使用内存
    private String usedMemory;
    //gc执行次数
    private long gcCount;
    //最近一次gc耗时
    private long gcAvgTime;
    //总线程数量
    private int activeThreadCount;
    //守护线程数量
    private int daemonThreadCount;
    //类加载数量
    private int classLoadedCount;
    //卸载类数量
    private long classUnloadedCount;

    public String getTotalMemory() {
        return totalMemory;
    }

    public void setTotalMemory(String totalMemory) {
        this.totalMemory = totalMemory;
    }

    public String getFreeMemory() {
        return freeMemory;
    }

    public void setFreeMemory(String freeMemory) {
        this.freeMemory = freeMemory;
    }

    public String getUsedMemory() {
        return usedMemory;
    }

    public void setUsedMemory(String usedMemory) {
        this.usedMemory = usedMemory;
    }

    public long getGcCount() {
        return gcCount;
    }

    public void setGcCount(long gcCount) {
        this.gcCount = gcCount;
    }

    public long getGcAvgTime() {
        return gcAvgTime;
    }

    public void setGcAvgTime(long gcAvgTime) {
        this.gcAvgTime = gcAvgTime;
    }

    public long getActiveThreadCount() {
        return activeThreadCount;
    }

    public void setActiveThreadCount(int activeThreadCount) {
        this.activeThreadCount = activeThreadCount;
    }

    public int getDaemonThreadCount() {
        return daemonThreadCount;
    }

    public void setDaemonThreadCount(int daemonThreadCount) {
        this.daemonThreadCount = daemonThreadCount;
    }

    public int getClassLoadedCount() {
        return classLoadedCount;
    }

    public void setClassLoadedCount(int classLoadedCount) {
        this.classLoadedCount = classLoadedCount;
    }

    public long getClassUnloadedCount() {
        return classUnloadedCount;
    }

    public void setClassUnloadedCount(long classUnloadedCount) {
        this.classUnloadedCount = classUnloadedCount;
    }
}
public class JvmMonitor {
    public static JvmRuntimeInfo getJvmRuntimeInfo() {
        Runtime runtime = Runtime.getRuntime();
        long gcCount = ManagementFactory.getGarbageCollectorMXBeans().parallelStream().mapToLong(mxBean -> mxBean.getCollectionCount()).sum();
        long gcTime = ManagementFactory.getGarbageCollectorMXBeans().parallelStream().mapToLong(mxBean -> mxBean.getCollectionTime()).sum();
        return Builder.of(JvmRuntimeInfo.class)
                .with(JvmRuntimeInfo::setTotalMemory, storageConvert(runtime.totalMemory()))
                .with(JvmRuntimeInfo::setFreeMemory, storageConvert(runtime.freeMemory()))
                .with(JvmRuntimeInfo::setUsedMemory, storageConvert(runtime.totalMemory() - runtime.freeMemory()))
                .with(JvmRuntimeInfo::setGcCount, gcCount)
                .with(JvmRuntimeInfo::setGcAvgTime, gcCount == 0 ? gcTime : (gcTime / gcCount))
                .with(JvmRuntimeInfo::setActiveThreadCount, ManagementFactory.getThreadMXBean().getThreadCount())
                .with(JvmRuntimeInfo::setActiveThreadCount, ManagementFactory.getThreadMXBean().getDaemonThreadCount())
                .with(JvmRuntimeInfo::setClassLoadedCount, ManagementFactory.getClassLoadingMXBean().getLoadedClassCount())
                .with(JvmRuntimeInfo::setClassUnloadedCount, ManagementFactory.getClassLoadingMXBean().getUnloadedClassCount())
                .build();
    }

    private static String storageConvert(long size) {
        if (size < 1024) {
            return size + "B";
        } else if (size >= 1024 * 1024 * 1024 * 1024l) {
            return size / (1024 * 1024 * 1024 * 1024l) + "TB";
        } else if (size >= 1024 * 1024 * 1024l) {
            return size / (1024 * 1024 * 1024l) + "GB";
        } else if (size >= 1024 * 1024l) {
            return size / (1024 * 1024l) + "MB";
        } else if (size >= 1024) {
            return size / 1024 + "KB";
        }
        return "0B";
    }
}
public class Builder<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(Builder.class);

    public T instace;

    private Builder(T instace) {
        this.instace = instace;
    }

    public static <T> Builder<T> of(Class<T> tClass) {
        T instance = null;
        try {
            instance = tClass.getDeclaredConstructor().newInstance();
        } catch (InvocationTargetException e) {
            LOGGER.error(tClass.getName(), e);
        } catch (NoSuchMethodException e) {
            LOGGER.error(tClass.getName(), e);
        } catch (InstantiationException e) {
            LOGGER.error(tClass.getName(), e);
        } catch (IllegalAccessException e) {
            LOGGER.error(tClass.getName(), e);
        }
        return new Builder<T>(instance);
    }

    public <U> Builder<T> with(BiConsumer<T, U> setter, U value) {
        if (null != setter) {
            setter.accept(instace, value);
        }
        return this;
    }

    public T build() {
        return instace;
    }
}
public class JvmMonitorApplication {
    private static final Logger LOGGER = LoggerFactory.getLogger(JvmMonitorApplication.class);

    public static void main(String[] args) {
        CompletableFuture.runAsync(() -> {
            while (true) {
                JvmRuntimeInfo jvmRuntimeInfo = JvmMonitor.getJvmRuntimeInfo();
                LOGGER.info(JSON.toJSONString(jvmRuntimeInfo));
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    LOGGER.error("monitor_exception", e);
                }
            }
        });
        while (true) {

        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JVM常用监控工具有很多,其中一个重要的工具就是dump分析工具。dump文件是指Java进程的内存快照,可以用于分析Java进程的内存使用情况,了解Java进程内部的情况。 下面介绍几个常用的dump分析工具: 1. jmap jmap是JDK自带的一个命令行工具,可以生成Java进程的内存快照。使用jmap生成dump文件的命令如下: ``` jmap -dump:format=b,file=<filename> <pid> ``` 其中,format=b表示生成二进制格式的dump文件,file=<filename>表示指定保存dump文件的路径和文件名,<pid>表示Java进程的进程ID。 2. jstack jstack也是JDK自带的一个命令行工具,可以打印Java进程的线程堆栈信息。使用jstack生成dump文件的命令如下: ``` jstack -F <pid> > <filename> ``` 其中,-F表示在进程不响应时强制获取线程堆栈信息,<pid>表示Java进程的进程ID,> <filename>表示将线程堆栈信息输出到指定文件。 3. VisualVM VisualVM是一个功能强大的Java监控和分析工具,可以监控和分析本地和远程Java进程。VisualVM可以生成Java进程的各种信息,包括dump文件。使用VisualVM生成dump文件的步骤如下: - 在VisualVM中打开需要生成dump文件的Java进程。 - 选择“Heap Dump”选项卡,点击“Heap Dump”按钮。 - 选择保存dump文件的路径和文件名,点击“Save”按钮。 4. Eclipse Memory Analyzer Eclipse Memory Analyzer是一款功能强大的Java内存分析工具,可以帮助开发人员分析Java进程的内存使用情况。Eclipse Memory Analyzer可以打开各种格式的dump文件,包括使用jmap、jstack和VisualVM生成的dump文件。 以上是常用的dump分析工具,可以帮助开发人员了解Java进程的内存使用情况。同时,需要注意的是,生成dump文件会对Java进程产生一定的影响,需要谨慎使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值