8154166: java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java fails with RuntimeException
authorhb
Thu, 05 May 2016 01:52:03 -0700
changeset 38346 5ed176fa7d97
parent 38345 c4e8f0e57f7b
child 38347 a3fdbd11148f
8154166: java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java fails with RuntimeException Summary: Fix the RuntimeException issue Reviewed-by: jbachorik
jdk/test/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java
--- a/jdk/test/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java	Fri Apr 29 22:39:44 2016 -0400
+++ b/jdk/test/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java	Thu May 05 01:52:03 2016 -0700
@@ -36,9 +36,13 @@
  * @modules java.management
  * @build jdk.testlibrary.* ResetPeakMemoryUsage MemoryUtil RunUtil
  * @run main ResetPeakMemoryUsage
+ * @requires vm.opt.ExplicitGCInvokesConcurrent != "true"
+ * @requires vm.opt.ExplicitGCInvokesConcurrentAndUnloadsClasses != "true"
+ * @requires vm.opt.DisableExplicitGC != "true"
  */
 
 import java.lang.management.*;
+import java.lang.ref.WeakReference;
 import java.util.*;
 
 public class ResetPeakMemoryUsage {
@@ -100,6 +104,7 @@
         printMemoryUsage(usage0, peak0);
 
         obj = new Object[largeArraySize];
+        WeakReference<Object> weakRef = new WeakReference<>(obj);
 
         MemoryUsage usage1 = mpool.getUsage();
         MemoryUsage peak1 = mpool.getPeakUsage();
@@ -124,7 +129,11 @@
         // The object is now garbage and do a GC
         // memory usage should drop
         obj = null;
-        mbean.gc();
+
+        //This will cause sure shot GC unlike Runtime.gc() invoked by mbean.gc()
+        while(weakRef.get() != null) {
+            mbean.gc();
+        }
 
         MemoryUsage usage2 = mpool.getUsage();
         MemoryUsage peak2 = mpool.getPeakUsage();