# HG changeset patch # User lmesnik # Date 1476860815 -10800 # Node ID 52488f6a3c4ad3027754168570fbae1689a26805 # Parent f8c527c9467f20cd3b2d838eb134f959f7741736 8166724: gc/g1/TestHumongousShrinkHeap.java fails with OOME Reviewed-by: dfazunen, jwilhelm diff -r f8c527c9467f -r 52488f6a3c4a test/gc/g1/TestHumongousShrinkHeap.java --- a/test/gc/g1/TestHumongousShrinkHeap.java Mon Sep 07 21:30:45 2015 +0400 +++ b/test/gc/g1/TestHumongousShrinkHeap.java Wed Oct 19 10:06:55 2016 +0300 @@ -49,23 +49,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(); }