get Full GC via MBeanServer - Java javax.management

Java examples for javax.management:MBeanServer

Description

get Full GC via MBeanServer

Demo Code


//package com.java2s;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;

public class Main {
    public static long getFullGC() {
        try {//from w  w w  . j  av a 2s  . c om
            MBeanServer mbeanServer = ManagementFactory
                    .getPlatformMBeanServer();
            ObjectName objectName;

            if (mbeanServer
                    .isRegistered(new ObjectName(
                            "java.lang:type=GarbageCollector,name=ConcurrentMarkSweep"))) {
                objectName = new ObjectName(
                        "java.lang:type=GarbageCollector,name=ConcurrentMarkSweep");
            } else if (mbeanServer
                    .isRegistered(new ObjectName(
                            "java.lang:type=GarbageCollector,name=MarkSweepCompact"))) {
                objectName = new ObjectName(
                        "java.lang:type=GarbageCollector,name=MarkSweepCompact");
            } else {
                objectName = new ObjectName(
                        "java.lang:type=GarbageCollector,name=PS MarkSweep");
            }
            return (Long) mbeanServer.getAttribute(objectName,
                    "CollectionCount");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials