Path: blob/master/test/hotspot/jtreg/gc/g1/TestPeriodicCollection.java
40942 views
/*1* Copyright (c) 2018, 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.g1;2425/**26* @test TestPeriodicCollection27* @requires vm.gc.G128* @requires vm.compMode != "Xcomp"29* @summary Verify that heap shrinks when the application is idle.30* @library /test/lib /31* @modules java.base/jdk.internal.misc32* @modules java.management/sun.management33* @run main/othervm -XX:MaxNewSize=32M -XX:InitialHeapSize=48M -Xmx128M -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=25 -XX:+UseG1GC -XX:G1PeriodicGCInterval=3000 -XX:+G1PeriodicGCInvokesConcurrent -Xlog:gc*,gc+periodic=debug,gc+ergo+heap=debug gc.g1.TestPeriodicCollection34* @run main/othervm -XX:MaxNewSize=32M -XX:InitialHeapSize=48M -Xmx128M -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=25 -XX:+UseG1GC -XX:G1PeriodicGCInterval=3000 -XX:-G1PeriodicGCInvokesConcurrent -Xlog:gc*,gc+periodic=debug,gc+ergo+heap=debug gc.g1.TestPeriodicCollection35*/3637import com.sun.management.HotSpotDiagnosticMXBean;3839import gc.testlibrary.Helpers;4041import java.lang.management.GarbageCollectorMXBean;42import java.lang.management.ManagementFactory;43import java.lang.management.MemoryUsage;44import java.text.NumberFormat;45import static jdk.test.lib.Asserts.*;4647public class TestPeriodicCollection {4849public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";50public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";5152private static final int IDLE_TIME = 7 * 1000;5354private static boolean gcOccurred() {55for (GarbageCollectorMXBean b : ManagementFactory.getGarbageCollectorMXBeans()) {56if (b.getCollectionCount() != 0) {57return true;58}59}60return false;61}6263public static void main(String[] args) {64MemoryUsage muInitial = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();65printMemoryUsage("initial", muInitial);6667if (gcOccurred()) {68System.out.println("At least one garbage collection occurred. Exiting as this may have already shrunk the heap.");69return;70}7172try {73Thread.sleep(IDLE_TIME);74} catch (InterruptedException ie) {75System.err.println("Skipped. Failed to wait for idle collection");76}7778MemoryUsage muAfter = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();79printMemoryUsage("after", muAfter);8081assertLessThan(muAfter.getCommitted(), muInitial.getCommitted(), String.format(82"committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"83+ "%s = %s%n%s = %s",84MIN_FREE_RATIO_FLAG_NAME,85ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)86.getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),87MAX_FREE_RATIO_FLAG_NAME,88ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)89.getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()90));91}9293public static final NumberFormat NF = Helpers.numberFormatter();9495public static void printMemoryUsage(String label, MemoryUsage memusage) {96float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();97System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",98label,99NF.format(memusage.getInit()),100NF.format(memusage.getUsed()),101NF.format(memusage.getCommitted()),102freeratio * 100103);104}105}106107108