Path: blob/master/test/hotspot/jtreg/runtime/ClassUnload/UnloadTest.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 UnloadTest25* @bug 821055926* @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 -Xlog:class+unload=debug UnloadTest33*/34import sun.hotspot.WhiteBox;35import jdk.test.lib.classloader.ClassUnloadCommon;3637/**38* Test that verifies that classes are unloaded when they are no longer reachable.39*40* The test creates a class loader, uses the loader to load a class and creates an instance41* of that class. The it nulls out all the references to the instance, class and class loader42* and tries to trigger class unloading. Then it verifies that the class is no longer43* loaded by the VM.44*/45public class UnloadTest {46private static String className = "test.Empty";4748public static void main(String... args) throws Exception {49run();50}5152private static void run() throws Exception {53final WhiteBox wb = WhiteBox.getWhiteBox();5455ClassUnloadCommon.failIf(wb.isClassAlive(className), "is not expected to be alive yet");5657ClassLoader cl = ClassUnloadCommon.newClassLoader();58Class<?> c = cl.loadClass(className);59Object o = c.newInstance();6061ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here");6263String loaderName = cl.getName();64int loadedRefcount = wb.getSymbolRefcount(loaderName);65System.out.println("Refcount of symbol " + loaderName + " is " + loadedRefcount);6667cl = null; c = null; o = null;68ClassUnloadCommon.triggerUnloading();69ClassUnloadCommon.failIf(wb.isClassAlive(className), "should have been unloaded");7071int unloadedRefcount = wb.getSymbolRefcount(loaderName);72System.out.println("Refcount of symbol " + loaderName + " is " + unloadedRefcount);73ClassUnloadCommon.failIf(unloadedRefcount != (loadedRefcount - 1), "Refcount must be decremented");74}75}767778