changeset 12193:99096bf79f31

8166724: gc/g1/TestHumongousShrinkHeap.java fails with OOME Reviewed-by: dfazunen, jwilhelm
author lmesnik
date Wed, 19 Oct 2016 10:06:55 +0300
parents 7e0181d2ca46
children 4c2ce2515ad0
files test/gc/g1/TestHumongousShrinkHeap.java
diffstat 1 files changed, 16 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/test/gc/g1/TestHumongousShrinkHeap.java	Wed Oct 19 10:10:51 2016 +0300
+++ b/test/gc/g1/TestHumongousShrinkHeap.java	Wed Oct 19 10:06:55 2016 +0300
@@ -51,23 +51,29 @@
     private static final int REGION_SIZE = 1024 * 1024; // 1M
     private static final int LISTS_COUNT = 10;
     private static final int HUMON_SIZE = Math.round(.9f * REGION_SIZE);
-    private static final long AVAILABLE_MEMORY
-                              = Runtime.getRuntime().freeMemory();
-    private static final int HUMON_COUNT
-                             = (int) ((AVAILABLE_MEMORY / HUMON_SIZE)
-            / LISTS_COUNT);
 
+    private static final long TOTAL_MEMORY = Runtime.getRuntime().totalMemory();
+    private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory();
+
+    private static final int HUMON_COUNT = (int) ((TOTAL_MEMORY / HUMON_SIZE) / LISTS_COUNT);
 
     public static void main(String[] args) {
         if (HUMON_COUNT == 0) {
             System.out.println("Skipped. Heap is too small");
             return;
         }
-        System.out.format("Running with %s max heap size. "
-                + "Will allocate humongous object of %s size %d times.%n",
-                MemoryUsagePrinter.humanReadableByteCount(AVAILABLE_MEMORY, false),
-                MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false),
-                HUMON_COUNT
+
+        if (TOTAL_MEMORY + REGION_SIZE * HUMON_COUNT > MAX_MEMORY) {
+            System.out.println("Skipped. Initial heap size is to close to max heap size.");
+            return;
+        }
+
+        System.out.format("Running with %s initial heap size of %s maximum heap size. "
+                          + "Will allocate humongous object of %s size %d times.%n",
+                          MemoryUsagePrinter.humanReadableByteCount(TOTAL_MEMORY, false),
+                          MemoryUsagePrinter.humanReadableByteCount(MAX_MEMORY, false),
+                          MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false),
+                          HUMON_COUNT
         );
         new TestHumongousShrinkHeap().test();
     }