--- old/src/hotspot/share/classfile/classLoaderData.cpp 2018-09-13 17:20:57.936694651 -0400 +++ new/src/hotspot/share/classfile/classLoaderData.cpp 2018-09-13 17:20:57.676694662 -0400 @@ -769,6 +769,14 @@ if (_deallocate_list != NULL) { delete _deallocate_list; } + + // Decrement refcounts of Symbols if created. + if (_name != NULL) { + _name->decrement_refcount(); + } + if (_name_and_id != NULL) { + _name_and_id->decrement_refcount(); + } } // Returns true if this class loader data is for the app class loader --- old/src/hotspot/share/prims/whitebox.cpp 2018-09-13 17:20:58.312694635 -0400 +++ new/src/hotspot/share/prims/whitebox.cpp 2018-09-13 17:20:58.056694646 -0400 @@ -178,6 +178,15 @@ return closure.found(); WB_END +WB_ENTRY(jint, WB_GetSymbolRefcount(JNIEnv* env, jobject unused, jstring name)) + oop h_name = JNIHandles::resolve(name); + if (h_name == NULL) return false; + Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_0); + TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return + return (jint)sym->refcount(); +WB_END + + WB_ENTRY(void, WB_AddToBootstrapClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) { #if INCLUDE_JVMTI ResourceMark rm; @@ -1982,7 +1991,6 @@ return (jint) SystemDictionary::pd_cache_table()->removed_entries_count(); WB_END - #define CC (char*) static JNINativeMethod methods[] = { @@ -1996,6 +2004,7 @@ {CC"getHeapSpaceAlignment", CC"()J", (void*)&WB_GetHeapSpaceAlignment}, {CC"getHeapAlignment", CC"()J", (void*)&WB_GetHeapAlignment}, {CC"isClassAlive0", CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive }, + {CC"getSymbolRefcount", CC"(Ljava/lang/String;)I", (void*)&WB_GetSymbolRefcount }, {CC"parseCommandLine0", CC"(Ljava/lang/String;C[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;", (void*) &WB_ParseCommandLine --- old/test/hotspot/jtreg/runtime/ClassUnload/UnloadTest.java 2018-09-13 17:20:58.700694619 -0400 +++ new/test/hotspot/jtreg/runtime/ClassUnload/UnloadTest.java 2018-09-13 17:20:58.448694630 -0400 @@ -23,6 +23,7 @@ /* * @test UnloadTest + * @bug 8210559 * @requires vm.opt.final.ClassUnloading * @modules java.base/jdk.internal.misc * @library /runtime/testlibrary /test/lib @@ -30,7 +31,7 @@ * @build sun.hotspot.WhiteBox test.Empty * @run driver ClassFileInstaller sun.hotspot.WhiteBox * sun.hotspot.WhiteBox$WhiteBoxPermission - * @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI UnloadTest + * @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xlog:class+unload=debug UnloadTest */ import sun.hotspot.WhiteBox; @@ -60,8 +61,16 @@ ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here"); + String loaderName = cl.getName(); + int loadedRefcount = wb.getSymbolRefcount(loaderName); + System.out.println("Refcount of symbol " + loaderName + " is " + loadedRefcount); + cl = null; c = null; o = null; ClassUnloadCommon.triggerUnloading(); ClassUnloadCommon.failIf(wb.isClassAlive(className), "should have been unloaded"); + + int unloadedRefcount = wb.getSymbolRefcount(loaderName); + System.out.println("Refcount of symbol " + loaderName + " is " + unloadedRefcount); + ClassUnloadCommon.failIf(unloadedRefcount != (loadedRefcount - 1), "Refcount must be decremented"); } } --- old/test/hotspot/jtreg/runtime/testlibrary/ClassUnloadCommon.java 2018-09-13 17:20:59.052694605 -0400 +++ new/test/hotspot/jtreg/runtime/testlibrary/ClassUnloadCommon.java 2018-09-13 17:20:58.796694615 -0400 @@ -73,7 +73,7 @@ .map(Paths::get) .map(ClassUnloadCommon::toURL) .toArray(URL[]::new); - return new URLClassLoader(urls) { + return new URLClassLoader("ClassUnloadCommonClassLoader", urls, new ClassUnloadCommon().getClass().getClassLoader()) { @Override public Class loadClass(String cn, boolean resolve) throws ClassNotFoundException --- old/test/lib/sun/hotspot/WhiteBox.java 2018-09-13 17:20:59.408694590 -0400 +++ new/test/lib/sun/hotspot/WhiteBox.java 2018-09-13 17:20:59.156694600 -0400 @@ -103,6 +103,7 @@ return isClassAlive0(name.replace('.', '/')); } private native boolean isClassAlive0(String name); + public native int getSymbolRefcount(String name); private native boolean isMonitorInflated0(Object obj); public boolean isMonitorInflated(Object obj) {