8196325: GarbageCollectionNotificationInfo has same information for before and after
authorsangheki
Tue, 24 Apr 2018 16:48:29 -0700
changeset 49878 2422d4e027b0
parent 49877 d84f06a0cae1
child 49879 601277b1d582
8196325: GarbageCollectionNotificationInfo has same information for before and after Reviewed-by: mchung, sspitsyn
src/java.management/share/classes/sun/management/ManagementFactoryHelper.java
src/jdk.management/share/classes/com/sun/management/internal/GarbageCollectorExtImpl.java
test/jdk/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java
--- a/src/java.management/share/classes/sun/management/ManagementFactoryHelper.java	Tue Apr 24 15:07:20 2018 -0700
+++ b/src/java.management/share/classes/sun/management/ManagementFactoryHelper.java	Tue Apr 24 16:48:29 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2018, 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
@@ -47,6 +47,7 @@
 
 import java.lang.reflect.UndeclaredThrowableException;
 import java.security.PrivilegedAction;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
@@ -162,6 +163,16 @@
         return LoggingMXBeanAccess.isAvailable();
     }
 
+    /**
+     * Returns an array of the name of all memory pools.  The order of the memory pools is
+     * significant and maintained in the VM.
+     */
+    public static String[] getAllMemoryPoolNames() {
+        return Arrays.stream(MemoryImpl.getMemoryPools())
+                .map(MemoryPoolMXBean::getName)
+                .toArray(String[]::new);
+    }
+
     // The LoggingMXBeanAccess class uses reflection to determine
     // whether java.util.logging is present, and load the actual LoggingMXBean
     // implementation.
--- a/src/jdk.management/share/classes/com/sun/management/internal/GarbageCollectorExtImpl.java	Tue Apr 24 15:07:20 2018 -0700
+++ b/src/jdk.management/share/classes/com/sun/management/internal/GarbageCollectorExtImpl.java	Tue Apr 24 16:48:29 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2018, 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
@@ -28,7 +28,6 @@
 import com.sun.management.GarbageCollectionNotificationInfo;
 import com.sun.management.GarbageCollectorMXBean;
 import com.sun.management.GcInfo;
-import java.lang.management.ManagementFactory;
 import java.lang.management.MemoryPoolMXBean;
 import java.util.List;
 import javax.management.ListenerNotFoundException;
@@ -38,6 +37,7 @@
 import javax.management.NotificationListener;
 import javax.management.openmbean.CompositeData;
 import sun.management.GarbageCollectorImpl;
+import sun.management.ManagementFactoryHelper;
 
 /**
  * Implementation class for the garbage collector.
@@ -59,12 +59,8 @@
     private String[] poolNames = null;
     private synchronized String[] getAllPoolNames() {
         if (poolNames == null) {
-            List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
-            poolNames = new String[pools.size()];
-            int i = 0;
-            for (MemoryPoolMXBean m : pools) {
-                poolNames[i++] = m.getName();
-            }
+            // The order of all memory pool names is important as GcInfo is also created with same order.
+            poolNames = ManagementFactoryHelper.getAllMemoryPoolNames();
         }
         return poolNames;
     }
--- a/test/jdk/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java	Tue Apr 24 15:07:20 2018 -0700
+++ b/test/jdk/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java	Tue Apr 24 16:48:29 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, 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
@@ -29,8 +29,8 @@
  * @requires vm.opt.ExplicitGCInvokesConcurrent == null | vm.opt.ExplicitGCInvokesConcurrent == false
  * @modules java.management/sun.management
  *          jdk.management
- * @run     main/othervm GarbageCollectionNotificationContentTest
- */
+ * @run     main/othervm -Xms64m -Xmx64m GarbageCollectionNotificationContentTest
+  */
 
 import java.util.*;
 import java.lang.management.*;
@@ -97,7 +97,7 @@
         System.gc();
         // Allocation of many short living and small objects to trigger minor GC
         Object data[] = new Object[32];
-        for(int i = 0; i<100000000; i++) {
+        for(int i = 0; i<10000000; i++) {
             data[i%32] = new int[8];
         }
         int wakeup = 0;
@@ -139,6 +139,8 @@
             System.out.println("Usage for pool " + poolname);
             System.out.println("   Before GC: " + busage);
             System.out.println("   After GC: " + ausage);
+
+            checkMemoryUsage(poolname, busage, ausage);
         }
 
         // check if memory usage for all memory pools are returned
@@ -150,4 +152,13 @@
             }
         }
     }
+
+    private static void checkMemoryUsage(String poolname, MemoryUsage busage, MemoryUsage ausage) throws Exception {
+        if (poolname.contains("Eden Space") && busage.getUsed() > 0) {
+            // Used size at Eden Space should be decreased or
+            if (busage.getUsed() <= ausage.getUsed()) {
+                throw new RuntimeException("Used size at Eden Space should be decreased.");
+            }
+        }
+    }
 }