你能在 Java 中获得基本的 GC 统计信息吗?

2023-07-12Java开发问题
5

本文介绍了你能在 Java 中获得基本的 GC 统计信息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想让一些长时间运行的服务器应用程序定期输出 Java 中的一般 GC 性能数字,比如 Runtime.freeMemory() 等 GC 等值.比如完成的周期数、平均时间等.

I would like to have some long-running server applications periodically output general GC performance numbers in Java, something like the GC equivalent of Runtime.freeMemory(), etc. Something like number of cycles done, average time, etc.

我们的系统在客户机器上运行,怀疑配置错误的内存池会导致过多的 GC 频率和长度 - 我认为定期报告基本 GC 活动通常是好的.

We have systems running on customer machines where there is a suspicion that misconfigured memory pools are causing excessive GC frequency and length - it occurs to me that it would be good in general to periodically report the basic GC activity.

是否有任何独立于平台的方式来做到这一点?

Is there any platform independent way to do this?

我特别想在运行时将此数据输出到系统日志(控制台);这不是我想连接到 JVM 的东西,就像 JConsole 或 JVisualVM 一样.

Edit2:MX bean 看起来像我想要的 - 有没有人有一个获得其中之一的工作代码示例?

The MX bean looks like what I want - does anyone have a working code example which obtains one of these?

推荐答案

这是一个使用 GarbageCollectorMXBean 打印出 GC 统计信息.大概您会定期调用此方法,例如使用 ScheduledExecutorService 进行调度.

Here's an example using GarbageCollectorMXBean to print out GC stats. Presumably you would call this method periodically, e.g. scheduling using a ScheduledExecutorService.

public void printGCStats() {
    long totalGarbageCollections = 0;
    long garbageCollectionTime = 0;

    for(GarbageCollectorMXBean gc :
            ManagementFactory.getGarbageCollectorMXBeans()) {

        long count = gc.getCollectionCount();

        if(count >= 0) {
            totalGarbageCollections += count;
        }

        long time = gc.getCollectionTime();

        if(time >= 0) {
            garbageCollectionTime += time;
        }
    }

    System.out.println("Total Garbage Collections: "
        + totalGarbageCollections);
    System.out.println("Total Garbage Collection Time (ms): "
        + garbageCollectionTime);
}

这篇关于你能在 Java 中获得基本的 GC 统计信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13