Path: blob/master/test/hotspot/jtreg/gc/class_unloading/TestClassUnloadingDisabled.java
40942 views
/*1* Copyright (c) 2016, 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* @test TestClassUnloadingDisabledSerial27* @bug 811482328* @requires vm.opt.ExplicitGCInvokesConcurrent != true29* @requires vm.opt.ClassUnloading != true30* @requires vm.gc.Serial31* @library /test/lib32* @modules java.base/jdk.internal.misc33* java.management34* @build sun.hotspot.WhiteBox35* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox36*37* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI38* -XX:-ClassUnloading -XX:+UseSerialGC gc.class_unloading.TestClassUnloadingDisabled39*40*/4142/*43* @test TestClassUnloadingDisabledParallel44* @bug 811482345* @requires vm.opt.ExplicitGCInvokesConcurrent != true46* @requires vm.opt.ClassUnloading != true47* @requires vm.gc.Parallel48* @library /test/lib49* @modules java.base/jdk.internal.misc50* java.management51* @build sun.hotspot.WhiteBox52* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox53*54* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI55* -XX:-ClassUnloading -XX:+UseParallelGC gc.class_unloading.TestClassUnloadingDisabled56*57*/5859/*60* @test TestClassUnloadingDisabledG161* @bug 811482362* @requires vm.opt.ExplicitGCInvokesConcurrent != true63* @requires vm.opt.ClassUnloading != true64* @requires vm.gc.G165* @library /test/lib66* @modules java.base/jdk.internal.misc67* java.management68* @build sun.hotspot.WhiteBox69* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox70*71* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI72* -XX:-ClassUnloading -XX:+UseG1GC gc.class_unloading.TestClassUnloadingDisabled73*74*/7576/*77* @test TestClassUnloadingDisabledShenandoah78* @bug 811482379* @requires vm.gc.Shenandoah80* @requires vm.opt.ExplicitGCInvokesConcurrent != true81* @requires vm.opt.ClassUnloading != true82* @library /test/lib83* @modules java.base/jdk.internal.misc84* java.management85* @build sun.hotspot.WhiteBox86* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox87* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI88* -XX:-ClassUnloading -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC gc.class_unloading.TestClassUnloadingDisabled89*/9091import java.io.File;92import java.io.IOException;93import java.nio.file.Files;94import java.nio.file.Path;95import java.nio.file.Paths;9697import sun.hotspot.WhiteBox;9899import static jdk.test.lib.Asserts.*;100101public class TestClassUnloadingDisabled {102public static void main(String args[]) throws Exception {103final WhiteBox wb = WhiteBox.getWhiteBox();104// Fetch the dir where the test class and the class105// to be loaded resides.106String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath();107String className = "gc.class_unloading.ClassToLoadUnload"; // can not use ClassToLoadUnload.class.getName() as that would load the class108109assertFalse(wb.isClassAlive(className), "Should not be loaded yet");110111// The NoPDClassLoader handles loading classes in the test directory112// and loads them without a protection domain, which in some cases113// keeps the class live regardless of marking state.114NoPDClassLoader nopd = new NoPDClassLoader(classDir);115nopd.loadClass(className);116117assertTrue(wb.isClassAlive(className), "Class should be loaded");118119// Clear the class-loader, class and object references to make120// class unloading possible.121nopd = null;122123System.gc();124assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded");125}126}127128class NoPDClassLoader extends ClassLoader {129String path;130131NoPDClassLoader(String path) {132this.path = path;133}134135public Class<?> loadClass(String name) throws ClassNotFoundException {136byte[] cls = null;137File f = new File(path,name + ".class");138139// Delegate class loading if class not present in the given140// directory.141if (!f.exists()) {142return super.loadClass(name);143}144145try {146Path path = Paths.get(f.getAbsolutePath());147cls = Files.readAllBytes(path);148} catch (IOException e) {149throw new ClassNotFoundException(name);150}151152// Define class with no protection domain and resolve it.153return defineClass(name, cls, 0, cls.length, null);154}155}156157class ClassToLoadUnload {158}159160161