久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

java中jmx怎么獲取fullgc次數(shù)

163次閱讀
沒有評論

共計 1312 個字符,預(yù)計需要花費(fèi) 4 分鐘才能閱讀完成。

在 Java 中使用 JMX(Java Management Extensions)可以通過 MBean 來獲取 Full GC 次數(shù)。以下是一種實(shí)現(xiàn)方法:

  1. 使用以下代碼創(chuàng)建一個 MBean 接口,包含獲取 Full GC 次數(shù)的方法:
public interface GCStatsMBean {long getFullGCCount();
}
  1. 創(chuàng)建一個實(shí)現(xiàn) MBean 接口的類,并實(shí)現(xiàn)獲取 Full GC 次數(shù)的方法:
public class GCStats implements GCStatsMBean {private long fullGCCount = 0;

    public long getFullGCCount() {return fullGCCount;
    }

    public void incrementFullGCCount() {fullGCCount++;}
}
  1. 將該類注冊為一個 MBean,并暴露給 JMX 服務(wù)器:
public static void main(String[] args) throws Exception {MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    GCStats gcStats = new GCStats();
    ObjectName name = new ObjectName("com.example:type=GCStats");
    mbs.registerMBean(gcStats, name);

    // 監(jiān)聽 GC 事件,并在發(fā)生 Full GC 時調(diào)用 incrementFullGCCount 方法 
    NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getGarbageCollectorMXBeans().get(0);
    emitter.addNotificationListener(new NotificationListener() {@Override
        public void handleNotification(Notification notification, Object handback) {if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
                if (info.getGcAction().equals("end of major GC")) {gcStats.incrementFullGCCount();
                }
            }
        }
    }, null, null);

    // 等待程序運(yùn)行 
    Thread.sleep(Long.MAX_VALUE);
}

通過以上方法,我們可以在 JMX 中獲取 Full GC 次數(shù),通過調(diào)用 GCStatsMBean 的 getFullGCCount 方法來獲取。

丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2024-03-19發(fā)表,共計1312字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 沙湾县| 丹东市| 阳曲县| 清流县| 田东县| 青冈县| 海城市| 类乌齐县| 远安县| 黔江区| 温泉县| 浦县| 双江| 仙游县| 杂多县| 和平区| 民和| 琼中| 那坡县| 大庆市| 雅安市| 微山县| 蒙自县| 旅游| 金秀| 军事| 安塞县| 米易县| 涟水县| 奉化市| 渭源县| 广安市| 开原市| 卢湾区| 弋阳县| 洪泽县| 长海县| 张北县| 西丰县| 女性| 靖宇县|