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