|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 Class unloading test with concurrent mark |
| 26 | + * @summary Make sure that verification during gc does not prevent class unloading |
| 27 | + * @bug 8280454 |
| 28 | + * @requires vm.gc.G1 |
| 29 | + * @requires vm.opt.final.ClassUnloading |
| 30 | + * @requires vm.opt.final.ClassUnloadingWithConcurrentMark |
| 31 | + * @modules java.base/jdk.internal.misc |
| 32 | + * @library /test/lib |
| 33 | + * @library classes |
| 34 | + * @build sun.hotspot.WhiteBox test.Empty |
| 35 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox |
| 36 | + * @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UseG1GC -XX:+VerifyDuringGC -XX:+AlwaysTenure -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xlog:gc,class+unload=debug UnloadTestWithVerifyDuringGC |
| 37 | + */ |
| 38 | +import sun.hotspot.WhiteBox; |
| 39 | +import jdk.test.lib.classloader.ClassUnloadCommon; |
| 40 | + |
| 41 | +/** |
| 42 | + * Test that verifies that classes are unloaded using concurrent mark with G1 when they are no |
| 43 | + * longer reachable even when -XX:+VerifyDuringGC is enabled |
| 44 | + * |
| 45 | + * The test creates a class loader, uses the loader to load a class and creates an instance |
| 46 | + * of that class. The it nulls out all the references to the instance, class and class loader |
| 47 | + * and tries to trigger class unloading using a concurrent mark. Then it verifies that the class |
| 48 | + * is no longer loaded by the VM. |
| 49 | + */ |
| 50 | +public class UnloadTestWithVerifyDuringGC { |
| 51 | + private static final WhiteBox wb = WhiteBox.getWhiteBox(); |
| 52 | + |
| 53 | + private static void waitUntilConcMarkFinished() throws Exception { |
| 54 | + while (wb.g1InConcurrentMark()) { |
| 55 | + try { |
| 56 | + Thread.sleep(1); |
| 57 | + } catch (InterruptedException e) { |
| 58 | + System.out.println("Got InterruptedException while waiting for concurrent mark to finish"); |
| 59 | + throw e; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static void triggerUnloadingWithConcurrentMark() throws Exception { |
| 65 | + // Try to unload classes using concurrent mark. First wait for any currently running concurrent |
| 66 | + // cycle. |
| 67 | + waitUntilConcMarkFinished(); |
| 68 | + wb.g1StartConcMarkCycle(); |
| 69 | + waitUntilConcMarkFinished(); |
| 70 | + } |
| 71 | + |
| 72 | + private static String className = "test.Empty"; |
| 73 | + |
| 74 | + public static void main(String... args) throws Exception { |
| 75 | + ClassUnloadCommon.failIf(wb.isClassAlive(className), "is not expected to be alive yet"); |
| 76 | + |
| 77 | + ClassLoader cl = ClassUnloadCommon.newClassLoader(); |
| 78 | + Class<?> c = cl.loadClass(className); |
| 79 | + Object o = c.newInstance(); |
| 80 | + |
| 81 | + ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here"); |
| 82 | + |
| 83 | + String loaderName = cl.getName(); |
| 84 | + int loadedRefcount = wb.getSymbolRefcount(loaderName); |
| 85 | + System.out.println("Refcount of symbol " + loaderName + " is " + loadedRefcount); |
| 86 | + |
| 87 | + // Move everything into the old gen so that concurrent mark can unload. |
| 88 | + wb.youngGC(); |
| 89 | + cl = null; c = null; o = null; |
| 90 | + triggerUnloadingWithConcurrentMark(); |
| 91 | + |
| 92 | + ClassUnloadCommon.failIf(wb.isClassAlive(className), "should have been unloaded"); |
| 93 | + |
| 94 | + int unloadedRefcount = wb.getSymbolRefcount(loaderName); |
| 95 | + System.out.println("Refcount of symbol " + loaderName + " is " + unloadedRefcount); |
| 96 | + ClassUnloadCommon.failIf(unloadedRefcount != (loadedRefcount - 1), "Refcount must be decremented"); |
| 97 | + } |
| 98 | +} |
0 commit comments