diff --git a/test/jdk/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java b/test/jdk/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java index be8329ac48f..9fdb1b06423 100644 --- a/test/jdk/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java +++ b/test/jdk/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -52,8 +52,8 @@ public class GarbageCollectionNotificationContentTest { private static HashMap listenerInvoked = new HashMap(); - static volatile long count = 0; - static volatile long number = 0; + static volatile long notificationReceivedCount = 0; + static volatile long numberOfGCMBeans = 0; static Object synchronizer = new Object(); static class GcListener implements NotificationListener { @@ -64,12 +64,12 @@ public void handleNotification(Notification notif, Object handback) { GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData()); String source = ((ObjectName)notif.getSource()).getCanonicalName(); synchronized(synchronizer) { - if(listenerInvoked.get(source) == null) { - listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),gcNotif); - count++; - if(count >= number) { - synchronizer.notify(); - } + if (listenerInvoked.get(source) == null) { + listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(), gcNotif); + notificationReceivedCount++; + if (notificationReceivedCount >= numberOfGCMBeans) { + synchronizer.notify(); + } } } } @@ -85,16 +85,14 @@ public static void main(String[] args) throws Exception { System.out.println("GC Notification not supported by the JVM, test skipped"); return; } - final ObjectName gcMXBeanPattern = - new ObjectName("java.lang:type=GarbageCollector,*"); - Set names = - mbs.queryNames(gcMXBeanPattern, null); + final ObjectName gcMXBeanPattern = new ObjectName("java.lang:type=GarbageCollector,*"); + Set names = mbs.queryNames(gcMXBeanPattern, null); if (names.isEmpty()) throw new Exception("Test incorrect: no GC MXBeans"); - number = names.size(); + numberOfGCMBeans = names.size(); for (ObjectName n : names) { - if(mbs.isInstanceOf(n,"javax.management.NotificationEmitter")) { - listenerInvoked.put(n.getCanonicalName(),null); + if (mbs.isInstanceOf(n, "javax.management.NotificationEmitter")) { + listenerInvoked.put(n.getCanonicalName(), null); GcListener listener = new GcListener(); mbs.addNotificationListener(n, listener, null, null); } @@ -112,7 +110,7 @@ public static void main(String[] args) throws Exception { } int wakeup = 0; synchronized(synchronizer) { - while(count != number) { + while(notificationReceivedCount != numberOfGCMBeans) { synchronizer.wait(10000); wakeup++; if(wakeup > 10) @@ -126,9 +124,9 @@ public static void main(String[] args) throws Exception { } private static void checkGarbageCollectionNotificationInfoContent(GarbageCollectionNotificationInfo notif) throws Exception { - System.out.println("GC notification for "+notif.getGcName()); - System.out.print("Action: "+notif.getGcAction()); - System.out.println(" Cause: "+notif.getGcCause()); + System.out.println("GC notification for " + notif.getGcName()); + System.out.print("Action: " + notif.getGcAction()); + System.out.println(" Cause: " + notif.getGcCause()); GcInfo info = notif.getGcInfo(); System.out.print("GC Info #" + info.getId()); System.out.print(" start:" + info.getStartTime()); @@ -136,6 +134,12 @@ private static void checkGarbageCollectionNotificationInfoContent(GarbageCollect System.out.println(" (" + info.getDuration() + "ms)"); Map usage = info.getMemoryUsageBeforeGc(); + // Check MemoryUsage is present. For all but No GC events, check Eden usage decreases: + boolean doCheckMemoryUsage = true; + if (notif.getGcCause().equals("No GC")) { + System.out.println("(skip memory usage check for event with 'No GC' cause)"); + doCheckMemoryUsage = false; + } List pnames = new ArrayList(); for (Map.Entry entry : usage.entrySet() ) { String poolname = (String) entry.getKey(); @@ -143,22 +147,22 @@ private static void checkGarbageCollectionNotificationInfoContent(GarbageCollect MemoryUsage busage = (MemoryUsage) entry.getValue(); MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname); if (ausage == null) { - throw new RuntimeException("After Gc Memory does not exist" + - " for " + poolname); + throw new RuntimeException("After Gc Memory does not exist for " + poolname); } System.out.println("Usage for pool " + poolname); System.out.println(" Before GC: " + busage); System.out.println(" After GC: " + ausage); - checkMemoryUsage(poolname, busage, ausage); + if (doCheckMemoryUsage) { + checkMemoryUsage(poolname, busage, ausage); + } } // check if memory usage for all memory pools are returned List pools = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean p : pools ) { if (!pnames.contains(p.getName())) { - throw new RuntimeException("GcInfo does not contain " + - "memory usage for pool " + p.getName()); + throw new RuntimeException("GcInfo does not contain memory usage for pool " + p.getName()); } } }