Path: blob/master/test/hotspot/jtreg/runtime/ClassUnload/KeepAliveClassLoader.java
40942 views
/*1* Copyright (c) 2013, 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*/2223/*24* @test KeepAliveClassLoader25* @summary This test case uses a java.lang.ClassLoader instance to keep a class alive.26* @requires vm.opt.final.ClassUnloading27* @modules java.base/jdk.internal.misc28* @library /test/lib29* @library classes30* @build sun.hotspot.WhiteBox test.Empty31* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox32* @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI KeepAliveClassLoader33*/3435import sun.hotspot.WhiteBox;36import jdk.test.lib.classloader.ClassUnloadCommon;3738/**39* Test that verifies that classes are not unloaded when specific types of references are kept to them.40*/41public class KeepAliveClassLoader {42private static final String className = "test.Empty";43private static final WhiteBox wb = WhiteBox.getWhiteBox();44public static Object escape = null;454647public static void main(String... args) throws Exception {48ClassLoader cl = ClassUnloadCommon.newClassLoader();49Class<?> c = cl.loadClass(className);50Object o = c.newInstance();51o = null; c = null;52escape = cl;5354{55boolean isAlive = wb.isClassAlive(className);56System.out.println("testClassLoader (1) alive: " + isAlive);5758ClassUnloadCommon.failIf(!isAlive, "should be alive");59}60ClassUnloadCommon.triggerUnloading();6162{63boolean isAlive = wb.isClassAlive(className);64System.out.println("testClassLoader (2) alive: " + isAlive);6566ClassUnloadCommon.failIf(!isAlive, "should be alive");67}68cl = null;69escape = null;70ClassUnloadCommon.triggerUnloading();7172{73boolean isAlive = wb.isClassAlive(className);74System.out.println("testClassLoader (3) alive: " + isAlive);75ClassUnloadCommon.failIf(isAlive, "should be unloaded");76}7778}79}808182