Path: blob/master/test/hotspot/jtreg/gc/parallel/TestDynShrinkHeap.java
40942 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package gc.parallel;2425/**26* @test TestDynShrinkHeap27* @bug 801647928* @requires vm.gc.Parallel & os.maxMemory > 1G29* @summary Verify that the heap shrinks after full GC according to the current values of the Min/MaxHeapFreeRatio flags30* @modules java.base/jdk.internal.misc31* @modules jdk.management32* @library /test/lib /33* @run main/othervm -XX:+UseAdaptiveSizePolicyWithSystemGC -XX:+UseParallelGC -XX:MinHeapFreeRatio=0 -XX:MaxHeapFreeRatio=100 -Xmx1g -verbose:gc gc.parallel.TestDynShrinkHeap34*/35import jdk.test.lib.management.DynamicVMOption;36import java.lang.management.ManagementFactory;37import java.lang.management.MemoryUsage;38import java.util.ArrayList;39import java.text.NumberFormat;40import static jdk.test.lib.Asserts.assertLessThan;41import com.sun.management.HotSpotDiagnosticMXBean;42import gc.testlibrary.Helpers;4344public class TestDynShrinkHeap {4546public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";47public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";4849private static ArrayList<byte[]> list = new ArrayList<>(0);50private static final int LEN = 512 * 1024 + 1;5152public TestDynShrinkHeap() {53}5455private final void test() {56System.gc();57MemoryUsagePrinter.printMemoryUsage("init");5859eat();60MemoryUsagePrinter.printMemoryUsage("eaten");61MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();6263free();64MemoryUsagePrinter.printMemoryUsage("free");65MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();6667assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format(68"committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"69+ "%s = %s%n%s = %s",70MIN_FREE_RATIO_FLAG_NAME,71ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)72.getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),73MAX_FREE_RATIO_FLAG_NAME,74ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)75.getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()76));77}7879private void eat() {80for (int i = 0; i < LEN; i++) {81list.add(new byte[1024]);82}83MemoryUsagePrinter.printMemoryUsage("allocated " + LEN + " arrays");8485list.subList(0, LEN / 2).clear();86System.gc();87MemoryUsagePrinter.printMemoryUsage("array halved");88}8990private void free() {91int min = DynamicVMOption.getInt(MIN_FREE_RATIO_FLAG_NAME);92DynamicVMOption.setInt(MAX_FREE_RATIO_FLAG_NAME, min);93System.gc();94MemoryUsagePrinter.printMemoryUsage("under pressure");95}9697public static void main(String[] args) {98new TestDynShrinkHeap().test();99}100}101102/**103* Prints memory usage to standard output104*/105class MemoryUsagePrinter {106107public static final NumberFormat NF = Helpers.numberFormatter();108109public static void printMemoryUsage(String label) {110MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();111float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();112System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",113label,114NF.format(memusage.getInit()),115NF.format(memusage.getUsed()),116NF.format(memusage.getCommitted()),117freeratio * 100118);119}120}121122123