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