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.util.ArrayList; 36 37 public class TestDynShrinkHeap extends TestShrinkHeap { 38 39 public static ArrayList<byte[]> list = new ArrayList<>(0); 40 public static final int M = 1024 * 1024; // to make heap more manageable by test code 41 42 private final TestDynamicVMOption maxRatioOption; 43 private final TestDynamicVMOption minRatioOption; 44 45 public TestDynShrinkHeap() { 46 super(); 47 minRatioOption = new TestDynamicVMOption(MIN_FREE_RATIO_FLAG_NAME); 48 maxRatioOption = new TestDynamicVMOption(MAX_FREE_RATIO_FLAG_NAME); 49 50 // with maxHeapFreeRatio = 100 heap will not shrink at all, so 51 if (maxRatioOption.getIntValue() == 100) { 52 maxRatioOption.setIntValue(95); 53 } 54 } 55 56 protected void eat() { 57 for (int i = 0; i < M; i++) { 58 list.add(new byte[1024]); 59 } 60 MemoryUsagePrinter.printMemoryUsage("allocated " + M + " arrays"); 61 62 list.subList(0, M / 2).clear(); 63 System.gc(); 64 MemoryUsagePrinter.printMemoryUsage("array halfed"); 65 } 66 67 protected void free() { 68 maxRatioOption.setIntValue(minRatioOption.getIntValue() + 1); 69 System.gc(); 70 MemoryUsagePrinter.printMemoryUsage("under pressure"); 71 } 72 73 public static void main(String[] args) { 74 new TestDynShrinkHeap().test(); 75 } 76 }