Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/ClassUnload/UnloadTest.java
32284 views
/*1* Copyright (c) 2013, 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* @library /runtime/testlibrary /testlibrary /testlibrary/whitebox26* @library classes27* @build ClassUnloadCommon test.Empty28* @build UnloadTest29* @run main ClassFileInstaller sun.hotspot.WhiteBox30* @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI UnloadTest31*/32import sun.hotspot.WhiteBox;3334/**35* Test that verifies that classes are unloaded when they are no longer reachable.36*37* The test creates a class loader, uses the loader to load a class and creates an instance38* of that class. The it nulls out all the references to the instance, class and class loader39* and tries to trigger class unloading. Then it verifies that the class is no longer40* loaded by the VM.41*/42public class UnloadTest {43private static String className = "test.Empty";4445public static void main(String... args) throws Exception {46run();47}4849private static void run() throws Exception {50final WhiteBox wb = WhiteBox.getWhiteBox();5152ClassUnloadCommon.failIf(wb.isClassAlive(className), "is not expected to be alive yet");5354ClassLoader cl = ClassUnloadCommon.newClassLoader();55Class<?> c = cl.loadClass(className);56Object o = c.newInstance();5758ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here");5960cl = null; c = null; o = null;61ClassUnloadCommon.triggerUnloading();62ClassUnloadCommon.failIf(wb.isClassAlive(className), "should have been unloaded");63}64}65666768