Thu, 11 Sep 2014 14:21:24 +0200
8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
Summary: Added respect for available memory. Renamed function names
Reviewed-by: jwilhelm, tschatzl
Contributed-by: andrey.x.zakharov@oracle.com
test/gc/g1/TestHumongousShrinkHeap.java | file | annotate | diff | comparison | revisions |
1.1 --- a/test/gc/g1/TestHumongousShrinkHeap.java Thu Sep 11 14:21:13 2014 +0200 1.2 +++ b/test/gc/g1/TestHumongousShrinkHeap.java Thu Sep 11 14:21:24 2014 +0200 1.3 @@ -26,7 +26,7 @@ 1.4 * @bug 8036025 8056043 1.5 * @summary Verify that heap shrinks after GC in the presence of fragmentation due to humongous objects 1.6 * @library /testlibrary 1.7 - * @run main/othervm -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=50 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc TestHumongousShrinkHeap 1.8 + * @run main/othervm -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=12 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc TestHumongousShrinkHeap 1.9 */ 1.10 1.11 import java.lang.management.ManagementFactory; 1.12 @@ -41,12 +41,24 @@ 1.13 public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio"; 1.14 public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio"; 1.15 1.16 - private static final ArrayList<ArrayList<byte[]>> garbage = new ArrayList<>(); 1.17 - private static final int PAGE_SIZE = 1024 * 1024; // 1M 1.18 - private static final int PAGES_NUM = 5; 1.19 + private static final List<List<byte[]>> garbage = new ArrayList(); 1.20 + private static final int REGION_SIZE = 1024 * 1024; // 1M 1.21 + private static final int LISTS_COUNT = 10; 1.22 + private static final int HUMON_SIZE = Math.round(.9f * REGION_SIZE); 1.23 + private static final long AVAILABLE_MEMORY 1.24 + = Runtime.getRuntime().freeMemory(); 1.25 + private static final int HUMON_COUNT 1.26 + = (int) ((AVAILABLE_MEMORY / HUMON_SIZE) 1.27 + / LISTS_COUNT); 1.28 1.29 1.30 public static void main(String[] args) { 1.31 + System.out.format("Running with %s max heap size. " 1.32 + + "Will allocate humongous object of %s size %d times.%n", 1.33 + MemoryUsagePrinter.humanReadableByteCount(AVAILABLE_MEMORY, false), 1.34 + MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false), 1.35 + HUMON_COUNT 1.36 + ); 1.37 new TestHumongousShrinkHeap().test(); 1.38 } 1.39 1.40 @@ -54,8 +66,8 @@ 1.41 System.gc(); 1.42 MemoryUsagePrinter.printMemoryUsage("init"); 1.43 1.44 - eat(); 1.45 - MemoryUsagePrinter.printMemoryUsage("eaten"); 1.46 + allocate(); 1.47 + MemoryUsagePrinter.printMemoryUsage("allocated"); 1.48 MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 1.49 1.50 free(); 1.51 @@ -72,15 +84,12 @@ 1.52 )); 1.53 } 1.54 1.55 - private void eat() { 1.56 - int HumongousObjectSize = Math.round(.9f * PAGE_SIZE); 1.57 - System.out.println("Will allocate objects of size=" + 1.58 - MemoryUsagePrinter.humanReadableByteCount(HumongousObjectSize, true)); 1.59 + private void allocate() { 1.60 1.61 - for (int i = 0; i < PAGES_NUM; i++) { 1.62 - ArrayList<byte[]> stuff = new ArrayList<>(); 1.63 - eatList(stuff, 100, HumongousObjectSize); 1.64 - MemoryUsagePrinter.printMemoryUsage("eat #" + i); 1.65 + for (int i = 0; i < LISTS_COUNT; i++) { 1.66 + List<byte[]> stuff = new ArrayList(); 1.67 + allocateList(stuff, HUMON_COUNT, HUMON_SIZE); 1.68 + MemoryUsagePrinter.printMemoryUsage("allocate #" + (i+1)); 1.69 garbage.add(stuff); 1.70 } 1.71 } 1.72 @@ -90,12 +99,12 @@ 1.73 garbage.subList(0, garbage.size() - 1).clear(); 1.74 1.75 // do not free last one element from last list 1.76 - ArrayList stuff = garbage.get(garbage.size() - 1); 1.77 + List stuff = garbage.get(garbage.size() - 1); 1.78 stuff.subList(0, stuff.size() - 1).clear(); 1.79 System.gc(); 1.80 } 1.81 1.82 - private static void eatList(List garbage, int count, int size) { 1.83 + private static void allocateList(List garbage, int count, int size) { 1.84 for (int i = 0; i < count; i++) { 1.85 garbage.add(new byte[size]); 1.86 } 1.87 @@ -122,9 +131,9 @@ 1.88 float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted(); 1.89 System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n", 1.90 label, 1.91 - humanReadableByteCount(memusage.getInit(), true), 1.92 - humanReadableByteCount(memusage.getUsed(), true), 1.93 - humanReadableByteCount(memusage.getCommitted(), true), 1.94 + humanReadableByteCount(memusage.getInit(), false), 1.95 + humanReadableByteCount(memusage.getUsed(), false), 1.96 + humanReadableByteCount(memusage.getCommitted(), false), 1.97 freeratio * 100 1.98 ); 1.99 }