Path: blob/master/test/hotspot/jtreg/gc/class_unloading/TestG1ClassUnloadingHWM.java
40942 views
/*1* Copyright (c) 2014, 2021, 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.class_unloading;2425/*26* @test27* @bug 804983128* @requires vm.gc.G129* @library /test/lib30* @modules java.base/jdk.internal.misc31* java.management32* @build sun.hotspot.WhiteBox33* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox34* @run driver gc.class_unloading.TestG1ClassUnloadingHWM35* @summary Test that -XX:-ClassUnloadingWithConcurrentMark will trigger a Full GC when more than MetaspaceSize metadata is allocated.36*/3738import jdk.test.lib.process.OutputAnalyzer;39import jdk.test.lib.process.ProcessTools;40import sun.hotspot.WhiteBox;4142public class TestG1ClassUnloadingHWM {43private static long MetaspaceSize = 32 * 1024 * 1024;44private static long YoungGenSize = 32 * 1024 * 1024;4546private static OutputAnalyzer run(boolean enableUnloading) throws Exception {47ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(48"-Xbootclasspath/a:.",49"-XX:+UnlockDiagnosticVMOptions",50"-XX:+WhiteBoxAPI",51"-XX:MetaspaceSize=" + MetaspaceSize,52"-Xmn" + YoungGenSize,53"-XX:+UseG1GC",54"-XX:" + (enableUnloading ? "+" : "-") + "ClassUnloadingWithConcurrentMark",55"-Xlog:gc",56TestG1ClassUnloadingHWM.AllocateBeyondMetaspaceSize.class.getName(),57"" + MetaspaceSize,58"" + YoungGenSize);59return new OutputAnalyzer(pb.start());60}6162public static OutputAnalyzer runWithG1ClassUnloading() throws Exception {63return run(true);64}6566public static OutputAnalyzer runWithoutG1ClassUnloading() throws Exception {67return run(false);68}6970public static void testWithoutG1ClassUnloading() throws Exception {71// -XX:-ClassUnloadingWithConcurrentMark is used, so we expect a full GC instead of a concurrent cycle.72OutputAnalyzer out = runWithoutG1ClassUnloading();7374out.shouldMatch(".*Pause Full.*");75out.shouldNotMatch(".*Pause Young \\(Concurrent Start\\).*");76}7778public static void testWithG1ClassUnloading() throws Exception {79// -XX:+ClassUnloadingWithConcurrentMark is used, so we expect a concurrent cycle instead of a full GC.80OutputAnalyzer out = runWithG1ClassUnloading();8182out.shouldMatch(".*Pause Young \\(Concurrent Start\\).*");83out.shouldNotMatch(".*Pause Full.*");84}8586public static void main(String args[]) throws Exception {87testWithG1ClassUnloading();88testWithoutG1ClassUnloading();89}9091public static class AllocateBeyondMetaspaceSize {92public static Object dummy;9394public static void main(String [] args) throws Exception {95if (args.length != 2) {96throw new IllegalArgumentException("Usage: <MetaspaceSize> <YoungGenSize>");97}9899WhiteBox wb = WhiteBox.getWhiteBox();100101// Allocate past the MetaspaceSize limit102long metaspaceSize = Long.parseLong(args[0]);103long allocationBeyondMetaspaceSize = metaspaceSize * 2;104105// There is a cap on how large a single metaspace allocation can get. So we may have to allocate in blocks.106final long max = wb.maxMetaspaceAllocationSize();107while (allocationBeyondMetaspaceSize > 0) {108long s = max < allocationBeyondMetaspaceSize ? max : allocationBeyondMetaspaceSize;109wb.allocateMetaspace(null, s);110allocationBeyondMetaspaceSize -= s;111}112113long youngGenSize = Long.parseLong(args[1]);114triggerYoungGCs(youngGenSize);115116}117118public static void triggerYoungGCs(long youngGenSize) {119long approxAllocSize = 32 * 1024;120long numAllocations = 2 * youngGenSize / approxAllocSize;121122for (long i = 0; i < numAllocations; i++) {123dummy = new byte[(int)approxAllocSize];124}125}126}127}128129130131