com.sun.management.GarbageCollectorMXBean Java Examples

The following examples show how to use com.sun.management.GarbageCollectorMXBean. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JmxClient.java    From vjtools with Apache License 2.0 6 votes vote down vote up
private synchronized void initGarbageCollector() throws IOException {
	if (fullGarbageCollectorMXBean != null || youngGarbageCollectorMXBean != null) {
		return;
	}

	List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getPlatformMXBeans(server,
			GarbageCollectorMXBean.class);
	A: for (GarbageCollectorMXBean gcollector : garbageCollectorMXBeans) {
		String[] memoryPoolNames = gcollector.getMemoryPoolNames();
		for (String poolName : memoryPoolNames) {
			if (poolName.toLowerCase().contains(JmxMemoryPoolManager.OLD)
					|| poolName.toLowerCase().contains(JmxMemoryPoolManager.TENURED)) {
				fullGarbageCollectorMXBean = gcollector;
				continue A;
			}
		}
		youngGarbageCollectorMXBean = gcollector;
	}
}
 
Example #2
Source File: AllocationStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs JFR recording, GC provocation/detection and stacktrace
 * verification for JFR event. In case of verification failure
 * repeats several times.
 *
 * @param bean MX bean for desired GC
 * @param memory allocator for desired type of allocations
 * @param expectedStack array of expected frames
 */
private static void testAllocEvent(GarbageCollectorMXBean bean, MemoryAllocator memory, String[] expectedStack) throws Exception {
    // The test checks the stacktrace of events and expects all the events are fired
    // in the current thread. But compilation may also trigger GC.
    // So to filter out such cases the test performs several iterations and expects
    // that the memory allocations made by the test will produce the desired JFR event.
    final int iterations = 5;
    for (int i = 0; i < iterations; i++) {
        if (allocAndCheck(bean, memory, expectedStack)) {
            return;
        } else {
            System.out.println("Attempt: " + i + " out of " + iterations+": no matching stack trace found.");
        }
        memory.clear();
    }
    throw new AssertionError("No matching stack trace found");
}
 
Example #3
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs JFR recording, GC provocation/detection and stacktrace
 * verification for JFR event. In case of verification failure
 * repeats several times.
 *
 * @param bean MX bean for desired GC
 * @param memory allocator for desired type of allocations
 * @param expectedStack array of expected frames
 */
private static void testAllocEvent(GarbageCollectorMXBean bean, MemoryAllocator memory, String[] expectedStack) throws Exception {
    // The test checks the stacktrace of events and expects all the events are fired
    // in the current thread. But compilation may also trigger GC.
    // So to filter out such cases the test performs several iterations and expects
    // that the memory allocations made by the test will produce the desired JFR event.
    final int iterations = 5;
    for (int i = 0; i < iterations; i++) {
        if (allocAndCheck(bean, memory, expectedStack)) {
            return;
        } else {
            System.out.println("Attempt: " + i + " out of " + iterations+": no matching stack trace found.");
        }
        memory.clear();
    }
    throw new AssertionError("No matching stack trace found");
}
 
Example #4
Source File: JmxGarbageCollectorManager.java    From vjtools with Apache License 2.0 6 votes vote down vote up
public JmxGarbageCollectorManager(MBeanServerConnection connection) throws IOException {
	if (ygcMXBean != null || fgcMXBean != null) {
		return;
	}

	List<GarbageCollectorMXBean> gcMXBeans = ManagementFactory.getPlatformMXBeans(connection,
			GarbageCollectorMXBean.class);
	for (GarbageCollectorMXBean gcMXBean : gcMXBeans) {
		String gcName = gcMXBean.getName();
		if ("Copy".equals(gcName) || "PS Scavenge".equals(gcName) || "ParNew".equals(gcName)
				|| "G1 Young Generation".equals(gcName)) {
			ygcMXBean = gcMXBean;
		} else if ("MarkSweepCompact".equals(gcName) || "PS MarkSweep".equals(gcName)
				|| "ConcurrentMarkSweep".equals(gcName) || "G1 Old Generation".equals(gcName)) {
			fgcMXBean = gcMXBean;
		} else {
			ygcMXBean = gcMXBean;
		}
	}
}
 
Example #5
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for old GC if -XX:+UseSerialGC is used
 */
public static void testMarkSweepCompactAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("MarkSweepCompact");
    MemoryAllocator memory = new OldGenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testMarkSweepCompactAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #6
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace during metaspace GC threshold if -XX:+UseSerialGC
 * is used
 */
public static void testMetaspaceSerialGCAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("MarkSweepCompact");
    MemoryAllocator memory = new MetaspaceMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testMetaspaceSerialGCAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #7
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for young GC if -XX:+UseConcMarkSweepGC is used
 */
public static void testParNewAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("ParNew");
    MemoryAllocator memory = new EdenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testParNewAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #8
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace during metaspace GC threshold if
 * -XX:+UseConcMarkSweepGC is used
 */
public static void testMetaspaceConcMarkSweepGCAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("ConcurrentMarkSweep");
    MemoryAllocator memory = new MetaspaceMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testMetaspaceConcMarkSweepGCAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #9
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for GC caused by humongous allocations if
 * -XX:+UseG1GC is used
 */
public static void testG1HumonAllocEvent() throws Exception {
    // G1 tries to reclaim humongous objects at every young collection
    // after doing a conservative estimate of its liveness
    GarbageCollectorMXBean bean = garbageCollectorMXBean("G1 Young Generation");
    MemoryAllocator memory = new HumongousMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testG1HumonAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #10
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for young GC if -XX:+UseParallelGC is used
 */
public static void testParallelScavengeAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("PS Scavenge");
    MemoryAllocator memory = new EdenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testParallelScavengeAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #11
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for old GC if -XX:+UseParallelGC is used
 */
public static void testParallelMarkSweepAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("PS MarkSweep");
    MemoryAllocator memory = new OldGenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testParallelMarkSweepAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #12
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace during metaspace GC threshold if
 * -XX:+UseParallelGC is used
 */
public static void testMetaspaceParallelGCAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("PS MarkSweep");
    MemoryAllocator memory = new MetaspaceMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testMetaspaceParallelGCAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #13
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for young GC if -XX:+UseG1GC is used
 */
public static void testG1YoungAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("G1 Young Generation");
    MemoryAllocator memory = new EdenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testG1YoungAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #14
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for young GC if -XX:+UseSerialGC is used
 */
public static void testDefNewAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("Copy");
    MemoryAllocator memory = new EdenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testDefNewAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #15
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace during metaspace GC threshold if -XX:+UseG1GC is
 * used
 */
public static void testMetaspaceG1GCAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("G1 Young Generation");
    MemoryAllocator memory = new MetaspaceMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testMetaspaceG1GCAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #16
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for old GC if -XX:+UseG1GC is used
 */
public static void testG1OldAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("G1 Old Generation");
    MemoryAllocator memory = new OldGenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testG1OldAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #17
Source File: AllocationStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for old GC if -XX:+UseConcMarkSweepGC is used
 */
public static void testConcMarkSweepAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("ConcurrentMarkSweep");
    MemoryAllocator memory = new OldGenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testConcMarkSweepAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #18
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace during metaspace GC threshold if -XX:+UseG1GC is
 * used
 */
public static void testMetaspaceG1GCAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("G1 Young Generation");
    MemoryAllocator memory = new MetaspaceMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testMetaspaceG1GCAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #19
Source File: AllocationStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for old GC if -XX:+UseConcMarkSweepGC is used
 */
public static void testConcMarkSweepAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("ConcurrentMarkSweep");
    MemoryAllocator memory = new OldGenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testConcMarkSweepAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #20
Source File: MonitorLiveSetSize.java    From java-svc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	AttachUtils.printJVMVersion();
	String pid = AttachUtils.checkPid(args);
	JMXConnector connector = JMXConnectorFactory.connect(AttachUtils.startLocalAgent(pid));
	MBeanServerConnection connection = connector.getMBeanServerConnection();
	List<GarbageCollectorMXBean> mBeans = getGarbageCollectorMBeans(connection, getGCNames(connection));

	registerNotifications(connection, mBeans);
	printInitGCStats(mBeans);

	System.out.println("Waiting for GC notifications!");
	System.out.println("Press <enter> to exit!");
	System.in.read();
}
 
Example #21
Source File: MonitorLiveSetSize.java    From java-svc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void registerNotifications(MBeanServerConnection connection, List<GarbageCollectorMXBean> mBeans)
		throws InstanceNotFoundException, IOException {
	GCNotificationListener listener = new GCNotificationListener();
	for (GarbageCollectorMXBean mbean : mBeans) {
		connection.addNotificationListener(mbean.getObjectName(), listener, null, mbean);
	}
}
 
Example #22
Source File: MonitorLiveSetSize.java    From java-svc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void printLastGCInfo(GarbageCollectorMXBean mBean) {
	GcInfo lastGcInfo = mBean.getLastGcInfo();
	for (Entry<String, MemoryUsage> usage : lastGcInfo.getMemoryUsageAfterGc().entrySet()) {
		System.out.println(String.format("%-25s\tused=%9d kB\tmax=%9d kB", usage.getKey() + ":",
				toKB(usage.getValue().getUsed()), toKB(usage.getValue().getMax())));
	}
}
 
Example #23
Source File: MonitorLiveSetSize.java    From java-svc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static List<GarbageCollectorMXBean> getGarbageCollectorMBeans(
	MBeanServerConnection connection, Set<ObjectName> gcNames) throws IOException {
	List<GarbageCollectorMXBean> gcBeans = new ArrayList<GarbageCollectorMXBean>(gcNames.size());
	for (ObjectName name : gcNames) {
		gcBeans.add(ManagementFactory.newPlatformMXBeanProxy(connection, name.getCanonicalName(),
				GarbageCollectorMXBean.class));
	}
	return gcBeans;
}
 
Example #24
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for old GC if -XX:+UseSerialGC is used
 */
public static void testMarkSweepCompactAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("MarkSweepCompact");
    MemoryAllocator memory = new OldGenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testMarkSweepCompactAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #25
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace during metaspace GC threshold if -XX:+UseSerialGC
 * is used
 */
public static void testMetaspaceSerialGCAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("MarkSweepCompact");
    MemoryAllocator memory = new MetaspaceMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testMetaspaceSerialGCAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #26
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for GC caused by humongous allocations if
 * -XX:+UseG1GC is used
 */
public static void testG1HumonAllocEvent() throws Exception {
    // G1 tries to reclaim humongous objects at every young collection
    // after doing a conservative estimate of its liveness
    GarbageCollectorMXBean bean = garbageCollectorMXBean("G1 Young Generation");
    MemoryAllocator memory = new HumongousMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testG1HumonAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #27
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace during metaspace GC threshold if
 * -XX:+UseConcMarkSweepGC is used
 */
public static void testMetaspaceConcMarkSweepGCAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("ConcurrentMarkSweep");
    MemoryAllocator memory = new MetaspaceMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testMetaspaceConcMarkSweepGCAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #28
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for old GC if -XX:+UseParallelGC is used
 */
public static void testParallelMarkSweepAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("PS MarkSweep");
    MemoryAllocator memory = new OldGenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testParallelMarkSweepAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #29
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for young GC if -XX:+UseG1GC is used
 */
public static void testG1YoungAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("G1 Young Generation");
    MemoryAllocator memory = new EdenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testG1YoungAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}
 
Example #30
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests event stacktrace for old GC if -XX:+UseG1GC is used
 */
public static void testG1OldAllocEvent() throws Exception {
    GarbageCollectorMXBean bean = garbageCollectorMXBean("G1 Old Generation");
    MemoryAllocator memory = new OldGenMemoryAllocator();

    String[] expectedStack = new String[]{
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testAllocEvent",
        "jdk.jfr.event.gc.stacktrace.AllocationStackTrace.testG1OldAllocEvent"
    };

    testAllocEvent(bean, memory, expectedStack);
}