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