Path: blob/master/test/hotspot/jtreg/runtime/ClassUnload/KeepAliveSoftReference.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 KeepAliveSoftReference25* @summary This test case uses a java.lang.ref.SoftReference referencing a 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 KeepAliveSoftReference33*/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 KeepAliveSoftReference {43private static final String className = "test.Empty";44private static final WhiteBox wb = WhiteBox.getWhiteBox();4546public static void main(String... args) throws Exception {47ClassLoader cl = ClassUnloadCommon.newClassLoader();48Class<?> c = cl.loadClass(className);49Object o = c.newInstance();50SoftReference<Object> sr = new SoftReference(o);51o = null; c = null; cl = null;5253{54boolean isAlive = wb.isClassAlive(className);55System.out.println("testSoftReference (1) alive: " + isAlive);56boolean cleared = (sr.get() == null);57boolean shouldBeAlive = !cleared;58ClassUnloadCommon.failIf(isAlive != shouldBeAlive, "" + isAlive + " != " + shouldBeAlive);59}6061ClassUnloadCommon.triggerUnloading();6263{64boolean isAlive = wb.isClassAlive(className);65System.out.println("testSoftReference (2) alive: " + isAlive);66boolean cleared = (sr.get() == null);67boolean shouldBeAlive = !cleared;68ClassUnloadCommon.failIf(isAlive != shouldBeAlive, "" + isAlive + " != " + shouldBeAlive);69}70sr.clear();71ClassUnloadCommon.triggerUnloading();7273{74boolean isAlive = wb.isClassAlive(className);75System.out.println("testSoftReference (3) alive: " + isAlive);76boolean cleared = (sr.get() == null);77boolean shouldBeAlive = !cleared;78ClassUnloadCommon.failIf(isAlive != shouldBeAlive, "" + isAlive + " != " + shouldBeAlive);79}80}81}828384