changeset 14466:d0b8cc47c117

8154166: java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java fails with RuntimeException Summary: Fix the RuntimeException issue Reviewed-by: jbachorik
author hb
date Thu, 05 May 2016 01:52:03 -0700
parents 37b8d7571efd
children d2f46fdfc3ca
files test/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java
diffstat 1 files changed, 10 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java	Fri Apr 29 22:39:44 2016 -0400
+++ b/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();