Path: blob/master/test/hotspot/jtreg/runtime/ClassUnload/KeepAliveClass.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 KeepAliveClass25* @summary This test case uses a java.lang.Class 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 KeepAliveClass33*/3435import java.lang.ref.SoftReference;36import sun.hotspot.WhiteBox;37import jdk.test.lib.classloader.ClassUnloadCommon;3839/**40* Test that verifies that classes are not unloaded when specific types of references are kept to them.41*/42public class KeepAliveClass {43private static final String className = "test.Empty";44private static final WhiteBox wb = WhiteBox.getWhiteBox();45public static Object escape = null;4647public static void main(String... args) throws Exception {48ClassLoader cl = ClassUnloadCommon.newClassLoader();49Class<?> c = cl.loadClass(className);50Object o = c.newInstance();51o = null; cl = null;52escape = c;5354{55boolean isAlive = wb.isClassAlive(className);56System.out.println("testClass (1) alive: " + isAlive);5758ClassUnloadCommon.failIf(!isAlive, "should be alive");59}6061ClassUnloadCommon.triggerUnloading();6263{64boolean isAlive = wb.isClassAlive(className);65System.out.println("testClass (2) alive: " + isAlive);6667ClassUnloadCommon.failIf(!isAlive, "should be alive");68}69c = null;70escape = null;71ClassUnloadCommon.triggerUnloading();7273{74boolean isAlive = wb.isClassAlive(className);75System.out.println("testClass (3) alive: " + isAlive);76ClassUnloadCommon.failIf(isAlive, "should be unloaded");77}7879}80}818283