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