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