1 /* 2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test TestHumongousShrinkHeap 26 * @bug 8036025 8056043 27 * @requires vm.gc=="G1" | vm.gc=="null" 28 * @summary Verify that heap shrinks after GC in the presence of fragmentation 29 * due to humongous objects 30 * @library /testlibrary 31 * @modules java.management/sun.management 32 * @run main/othervm -XX:-ExplicitGCInvokesConcurrent -XX:MinHeapFreeRatio=10 33 * -XX:MaxHeapFreeRatio=12 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc 34 * TestHumongousShrinkHeap 35 */ 36 37 import com.sun.management.HotSpotDiagnosticMXBean; 38 import java.lang.management.ManagementFactory; 39 import java.lang.management.MemoryUsage; 40 import java.util.ArrayList; 41 import java.util.List; 42 import static jdk.test.lib.Asserts.*; 43 44 public class TestHumongousShrinkHeap { 45 46 public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio"; 47 public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio"; 48 49 private static final List<List<byte[]>> garbage = new ArrayList(); 50 private static final int REGION_SIZE = 1024 * 1024; // 1M 51 private static final int LISTS_COUNT = 10; 52 private static final int HUMON_SIZE = Math.round(.9f * REGION_SIZE); 53 private static final long AVAILABLE_MEMORY 54 = Runtime.getRuntime().freeMemory(); 55 private static final int HUMON_COUNT 56 = (int) ((AVAILABLE_MEMORY / HUMON_SIZE) 57 / LISTS_COUNT); 58 59 60 public static void main(String[] args) { 61 System.out.format("Running with %s max heap size. " 62 + "Will allocate humongous object of %s size %d times.%n", 63 MemoryUsagePrinter.humanReadableByteCount(AVAILABLE_MEMORY, false), 64 MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false), 65 HUMON_COUNT 66 ); 67 new TestHumongousShrinkHeap().test(); 68 } 69 70 private final void test() { 71 System.gc(); 72 MemoryUsagePrinter.printMemoryUsage("init"); 73 74 allocate(); 75 MemoryUsagePrinter.printMemoryUsage("allocated"); 76 MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 77 78 free(); 79 MemoryUsagePrinter.printMemoryUsage("free"); 80 MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 81 82 assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format( 83 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n" 84 + "%s = %s%n%s = %s", 85 MIN_FREE_RATIO_FLAG_NAME, 86 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class) 87 .getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(), 88 MAX_FREE_RATIO_FLAG_NAME, 89 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class) 90 .getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue() 91 )); 92 } 93 94 private void allocate() { 95 96 for (int i = 0; i < LISTS_COUNT; i++) { 97 List<byte[]> stuff = new ArrayList(); 98 allocateList(stuff, HUMON_COUNT, HUMON_SIZE); 99 MemoryUsagePrinter.printMemoryUsage("allocate #" + (i+1)); 100 garbage.add(stuff); 101 } 102 } 103 104 private void free() { 105 // do not free last one list 106 garbage.subList(0, garbage.size() - 1).clear(); 107 108 // do not free last one element from last list 109 List stuff = garbage.get(garbage.size() - 1); 110 stuff.subList(0, stuff.size() - 1).clear(); 111 System.gc(); 112 } 113 114 private static void allocateList(List garbage, int count, int size) { 115 for (int i = 0; i < count; i++) { 116 garbage.add(new byte[size]); 117 } 118 } 119 } 120 121 /** 122 * Prints memory usage to standard output 123 */ 124 class MemoryUsagePrinter { 125 126 public static String humanReadableByteCount(long bytes, boolean si) { 127 int unit = si ? 1000 : 1024; 128 if (bytes < unit) { 129 return bytes + " B"; 130 } 131 int exp = (int) (Math.log(bytes) / Math.log(unit)); 132 String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); 133 return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); 134 } 135 136 public static void printMemoryUsage(String label) { 137 MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 138 float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted(); 139 System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n", 140 label, 141 humanReadableByteCount(memusage.getInit(), false), 142 humanReadableByteCount(memusage.getUsed(), false), 143 humanReadableByteCount(memusage.getCommitted(), false), 144 freeratio * 100 145 ); 146 } 147 }