--- old/src/hotspot/share/classfile/classLoaderData.cpp 2017-12-19 06:38:03.295762634 -0500 +++ new/src/hotspot/share/classfile/classLoaderData.cpp 2017-12-19 06:38:02.806530614 -0500 @@ -831,9 +831,10 @@ } // This is distinct from free_deallocate_list. For class loader data that are -// unloading, this frees the C heap memory for constant pools on the list. If there -// were C heap memory allocated for methods, it would free that too. The C heap memory -// for InstanceKlasses on this list is freed in the ClassLoaderData destructor. +// unloading, this frees the C heap memory for items on the list, and unlinks +// scratch or error classes so that unloading events aren't triggered for these +// classes. The metadata is removed with the unloading metaspace. +// There isn't C heap memory allocated for methods, so nothing is done for them. void ClassLoaderData::unload_deallocate_list() { // Don't need lock, at safepoint assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); @@ -849,6 +850,13 @@ // Only constant pool entries have C heap memory to free. if (m->is_constantPool()) { ((ConstantPool*)m)->release_C_heap_structures(); + } else if (m->is_klass()) { + InstanceKlass* ik = (InstanceKlass*)m; + // also releases ik->constants() C heap memory + InstanceKlass::release_C_heap_structures(ik); + // Remove the class so unloading events aren't triggered for + // this class (scratch or error class) in do_unloading(). + remove_class(ik); } } }