Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/class_unloading/TestG1ClassUnloadingHWM.java
32283 views
/*1* Copyright (c) 2014, 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*/2223/*24* @test25* @key gc26* @bug 804983127* @library /testlibrary /testlibrary/whitebox28* @build TestG1ClassUnloadingHWM29* @run main ClassFileInstaller sun.hotspot.WhiteBox30* @run driver TestG1ClassUnloadingHWM31* @summary Test that -XX:-ClassUnloadingWithConcurrentMark will trigger a Full GC when more than MetaspaceSize metadata is allocated.32*/3334import com.oracle.java.testlibrary.OutputAnalyzer;35import com.oracle.java.testlibrary.ProcessTools;36import java.util.ArrayList;37import java.util.Arrays;38import sun.hotspot.WhiteBox;3940public class TestG1ClassUnloadingHWM {41private static long MetaspaceSize = 32 * 1024 * 1024;42private static long YoungGenSize = 32 * 1024 * 1024;4344private static OutputAnalyzer run(boolean enableUnloading) throws Exception {45ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(46"-Xbootclasspath/a:.",47"-XX:+UnlockDiagnosticVMOptions",48"-XX:+WhiteBoxAPI",49"-XX:MetaspaceSize=" + MetaspaceSize,50"-Xmn" + YoungGenSize,51"-XX:+UseG1GC",52"-XX:" + (enableUnloading ? "+" : "-") + "ClassUnloadingWithConcurrentMark",53"-XX:+PrintHeapAtGC",54"-XX:+PrintGCDetails",55TestG1ClassUnloadingHWM.AllocateBeyondMetaspaceSize.class.getName(),56"" + MetaspaceSize,57"" + YoungGenSize);58return new OutputAnalyzer(pb.start());59}6061public static OutputAnalyzer runWithG1ClassUnloading() throws Exception {62return run(true);63}6465public static OutputAnalyzer runWithoutG1ClassUnloading() throws Exception {66return run(false);67}6869public static void testWithoutG1ClassUnloading() throws Exception {70// -XX:-ClassUnloadingWithConcurrentMark is used, so we expect a full GC instead of a concurrent cycle.71OutputAnalyzer out = runWithoutG1ClassUnloading();7273out.shouldMatch(".*Full GC.*");74out.shouldNotMatch(".*initial-mark.*");75}7677public static void testWithG1ClassUnloading() throws Exception {78// -XX:+ClassUnloadingWithConcurrentMark is used, so we expect a concurrent cycle instead of a full GC.79OutputAnalyzer out = runWithG1ClassUnloading();8081out.shouldMatch(".*initial-mark.*");82out.shouldNotMatch(".*Full GC.*");83}8485public static void main(String args[]) throws Exception {86testWithG1ClassUnloading();87testWithoutG1ClassUnloading();88}8990public static class AllocateBeyondMetaspaceSize {91public static Object dummy;9293public static void main(String [] args) throws Exception {94if (args.length != 2) {95throw new IllegalArgumentException("Usage: <MetaspaceSize> <YoungGenSize>");96}9798WhiteBox wb = WhiteBox.getWhiteBox();99100// Allocate past the MetaspaceSize limit101long metaspaceSize = Long.parseLong(args[0]);102long allocationBeyondMetaspaceSize = metaspaceSize * 2;103long metaspace = wb.allocateMetaspace(null, allocationBeyondMetaspaceSize);104105long youngGenSize = Long.parseLong(args[1]);106triggerYoungGCs(youngGenSize);107108wb.freeMetaspace(null, metaspace, metaspace);109}110111public static void triggerYoungGCs(long youngGenSize) {112long approxAllocSize = 32 * 1024;113long numAllocations = 2 * youngGenSize / approxAllocSize;114115for (long i = 0; i < numAllocations; i++) {116dummy = new byte[(int)approxAllocSize];117}118}119}120}121122123124