1 /*
2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test UnloadTest
26  * @bug 8210559
27  * @requires vm.opt.final.ClassUnloading
28  * @modules java.base/jdk.internal.misc
29  * @library /runtime/testlibrary /test/lib
30  * @library classes
31  * @build sun.hotspot.WhiteBox test.Empty
32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
33  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
34  * @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xlog:class+unload=debug UnloadTest
35  */
36 import sun.hotspot.WhiteBox;
37 
38 /**
39  * Test that verifies that classes are unloaded when they are no longer reachable.
40  *
41  * The test creates a class loader, uses the loader to load a class and creates an instance
42  * of that class. The it nulls out all the references to the instance, class and class loader
43  * and tries to trigger class unloading. Then it verifies that the class is no longer
44  * loaded by the VM.
45  */
46 public class UnloadTest {
47     private static String className = "test.Empty";
48 
main(String... args)49     public static void main(String... args) throws Exception {
50        run();
51     }
52 
run()53     private static void run() throws Exception {
54         final WhiteBox wb = WhiteBox.getWhiteBox();
55 
56         ClassUnloadCommon.failIf(wb.isClassAlive(className), "is not expected to be alive yet");
57 
58         ClassLoader cl = ClassUnloadCommon.newClassLoader();
59         Class<?> c = cl.loadClass(className);
60         Object o = c.newInstance();
61 
62         ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here");
63 
64         String loaderName = cl.getName();
65         int loadedRefcount = wb.getSymbolRefcount(loaderName);
66         System.out.println("Refcount of symbol " + loaderName + " is " + loadedRefcount);
67 
68         cl = null; c = null; o = null;
69         ClassUnloadCommon.triggerUnloading();
70         ClassUnloadCommon.failIf(wb.isClassAlive(className), "should have been unloaded");
71 
72         int unloadedRefcount = wb.getSymbolRefcount(loaderName);
73         System.out.println("Refcount of symbol " + loaderName + " is " + unloadedRefcount);
74         ClassUnloadCommon.failIf(unloadedRefcount != (loadedRefcount - 1), "Refcount must be decremented");
75     }
76 }
77