1 /* 2 * Copyright (c) 2014, 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 * @ignore 8041506, 8041946, 8042051 26 * @test TestHumongousShrinkHeap 27 * @bug 8036025 28 * @summary Verify that heap shrinks after GC in the presence of fragmentation due to humongous objects 29 * @library /testlibrary 30 * @run main/othervm -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=50 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc TestHumongousShrinkHeap 31 */ 32 33 import java.lang.management.ManagementFactory; 34 import java.lang.management.MemoryUsage; 35 import java.util.ArrayList; 36 import java.util.List; 37 import sun.management.ManagementFactoryHelper; 38 import static com.oracle.java.testlibrary.Asserts.*; 39 40 public class TestHumongousShrinkHeap { 41 42 public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio"; 43 public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio"; 44 45 private static final ArrayList<ArrayList<byte[]>> garbage = new ArrayList<>(); 46 private static final int PAGE_SIZE = 1024 * 1024; // 1M 47 private static final int PAGES_NUM = 5; 48 49 50 public static void main(String[] args) { 51 new TestHumongousShrinkHeap().test(); 52 } 53 54 private final void test() { 55 System.gc(); 56 MemoryUsagePrinter.printMemoryUsage("init"); 57 58 eat(); 59 MemoryUsagePrinter.printMemoryUsage("eaten"); 60 MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 61 62 free(); 63 MemoryUsagePrinter.printMemoryUsage("free"); 64 MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 65 66 assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format( 67 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n" 68 + "%s = %s%n%s = %s", 69 MIN_FREE_RATIO_FLAG_NAME, 70 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(), 71 MAX_FREE_RATIO_FLAG_NAME, 72 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue() 73 )); 74 } 75 76 private void eat() { 77 int HumongousObjectSize = Math.round(.9f * PAGE_SIZE); 78 System.out.println("Will allocate objects of size=" + 79 MemoryUsagePrinter.humanReadableByteCount(HumongousObjectSize, true)); 80 81 for (int i = 0; i < PAGES_NUM; i++) { 82 ArrayList<byte[]> stuff = new ArrayList<>(); 83 eatList(stuff, 100, HumongousObjectSize); 84 MemoryUsagePrinter.printMemoryUsage("eat #" + i); 85 garbage.add(stuff); 86 } 87 } 88 89 private void free() { 90 // do not free last one list 91 garbage.subList(0, garbage.size() - 1).clear(); 92 93 // do not free last one element from last list 94 ArrayList stuff = garbage.get(garbage.size() - 1); 95 stuff.subList(0, stuff.size() - 1).clear(); 96 System.gc(); 97 } 98 99 private static void eatList(List garbage, int count, int size) { 100 for (int i = 0; i < count; i++) { 101 garbage.add(new byte[size]); 102 } 103 } 104 } 105 106 /** 107 * Prints memory usage to standard output 108 */ 109 class MemoryUsagePrinter { 110 111 public static String humanReadableByteCount(long bytes, boolean si) { 112 int unit = si ? 1000 : 1024; 113 if (bytes < unit) { 114 return bytes + " B"; 115 } 116 int exp = (int) (Math.log(bytes) / Math.log(unit)); 117 String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); 118 return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); 119 } 120 121 public static void printMemoryUsage(String label) { 122 MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 123 float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted(); 124 System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n", 125 label, 126 humanReadableByteCount(memusage.getInit(), true), 127 humanReadableByteCount(memusage.getUsed(), true), 128 humanReadableByteCount(memusage.getCommitted(), true), 129 freeratio * 100 130 ); 131 } 132 }