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 * @test TestDynShrinkHeap 26 * @key gc 27 * @bug 8037924 28 * @summary Verify that heap shrinking after full gc and dynamically changed free ratio 29 * @author Andrey Zakharov /andrey.x.zakharov@oracle.com/ 30 * @library /testlibrary 31 * @run main/othervm -XX:+UseAdaptiveSizePolicyWithSystemGC -XX:+UseParallelGC -XX:MinHeapFreeRatio=1 32 * -XX:MaxHeapFreeRatio=100 -Xloggc:gc.log TestDynShrinkHeap 33 */ 34 import java.lang.management.ManagementFactory; 35 import java.lang.management.MemoryUsage; 36 import java.util.ArrayList; 37 import com.oracle.java.testlibrary.TestDynamicVMOption; 38 import com.oracle.java.testlibrary.MemoryUsagePrinter; 39 40 public class TestDynShrinkHeap { 41 42 public static final String MinFreeRatioFlagName = "MinHeapFreeRatio"; 43 public static final String MaxFreeRatioFlagName = "MaxHeapFreeRatio"; 44 45 public static ArrayList<byte[]> list = new ArrayList<byte[]>(0); 46 public static byte[] temp; 47 public static final int M = 1024 * 1024; // to make heap more manageable by test code 48 49 private final TestDynamicVMOption minRatioOption; 50 private final TestDynamicVMOption maxRatioOption; 51 52 public final class Labels { 53 public static final String beforeAllocation = "before allocation"; 54 public static final String arrayHalfed = "array halfed"; 55 public static final String underPressure = "under pressure"; 56 public static final String beforeGc = "before full GC"; 57 public static final String afterGc = "after full GC"; 58 59 } 60 61 62 public TestDynShrinkHeap() { 63 minRatioOption = new TestDynamicVMOption(MinFreeRatioFlagName); 64 maxRatioOption = new TestDynamicVMOption(MaxFreeRatioFlagName); 65 } 66 67 public void test() { 68 int minHeapFreeValue = minRatioOption.getIntValue(); 69 int maxHeapFreeValue = maxRatioOption.getIntValue(); 70 71 // with maxHeapFreeRatio = 100 heap will not shrink at all, so 72 if (maxHeapFreeValue == 100) { 73 maxHeapFreeValue = 95; 74 maxRatioOption.setIntValue(maxHeapFreeValue); 75 } 76 77 MemoryUsagePrinter.printMemoryUsage(Labels.beforeAllocation); 78 79 for (int i = 0; i < M; i++) { 80 list.add(new byte[1024]); 81 } 82 83 MemoryUsagePrinter.printMemoryUsage("allocated " + M + " arrays"); 84 85 list.subList(0, M / 2).clear(); 86 gc(); 87 MemoryUsagePrinter.printMemoryUsage(Labels.arrayHalfed); 88 MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 89 90 maxRatioOption.setIntValue(minHeapFreeValue + 1); 91 gc(); 92 MemoryUsagePrinter.printMemoryUsage(Labels.underPressure); 93 MemoryUsage muPressure = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 94 95 if (!(muPressure.getCommitted() < muFull.getCommitted())) { 96 StringBuilder strb = new StringBuilder("committed heap size under pressure is not less than committed full heap size, heap hasn't been shrunk?"); 97 strb.append(System.getProperty("line.separator")); 98 strb.append(MinFreeRatioFlagName + " = " + minHeapFreeValue); 99 strb.append(System.getProperty("line.separator")); 100 strb.append(MaxFreeRatioFlagName + " = " + maxHeapFreeValue); 101 throw new RuntimeException(strb.toString()); 102 } 103 } 104 105 public static void main(String[] args) { 106 new TestDynShrinkHeap().test(); 107 } 108 109 public static void gc() { 110 MemoryUsagePrinter.printMemoryUsage(Labels.beforeGc); 111 System.gc(); 112 MemoryUsagePrinter.printMemoryUsage(Labels.afterGc); 113 } 114 115 }