From 443afdc77ffb9184a985ec586c888eabe9ee8e6f Mon Sep 17 00:00:00 2001 From: Albert Mingkun Yang Date: Thu, 31 Jul 2025 18:52:44 +0000 Subject: [PATCH] 8364176: Serial: Group all class unloading logic at the end of marking phase Reviewed-by: tschatzl, sangheki --- src/hotspot/share/gc/serial/serialFullGC.cpp | 15 ++++++++++++--- src/hotspot/share/gc/serial/serialHeap.cpp | 10 ---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/hotspot/share/gc/serial/serialFullGC.cpp b/src/hotspot/share/gc/serial/serialFullGC.cpp index 6eacb08f218..19a8b81370f 100644 --- a/src/hotspot/share/gc/serial/serialFullGC.cpp +++ b/src/hotspot/share/gc/serial/serialFullGC.cpp @@ -525,7 +525,9 @@ void SerialFullGC::phase1_mark(bool clear_all_softrefs) { { GCTraceTime(Debug, gc, phases) tm_m("Class Unloading", gc_timer()); - ClassUnloadingContext* ctx = ClassUnloadingContext::context(); + ClassUnloadingContext ctx(1 /* num_nmethod_unlink_workers */, + false /* unregister_nmethods_during_purge */, + false /* lock_nmethod_free_separately */); bool unloading_occurred; { @@ -541,7 +543,7 @@ void SerialFullGC::phase1_mark(bool clear_all_softrefs) { { GCTraceTime(Debug, gc, phases) t("Purge Unlinked NMethods", gc_timer()); // Release unloaded nmethod's memory. - ctx->purge_nmethods(); + ctx.purge_nmethods(); } { GCTraceTime(Debug, gc, phases) ur("Unregister NMethods", gc_timer()); @@ -549,7 +551,7 @@ void SerialFullGC::phase1_mark(bool clear_all_softrefs) { } { GCTraceTime(Debug, gc, phases) t("Free Code Blobs", gc_timer()); - ctx->free_nmethods(); + ctx.free_nmethods(); } // Prune dead klasses from subklass/sibling/implementor lists. @@ -557,6 +559,13 @@ void SerialFullGC::phase1_mark(bool clear_all_softrefs) { // Clean JVMCI metadata handles. JVMCI_ONLY(JVMCI::do_unloading(unloading_occurred)); + + // Delete metaspaces for unloaded class loaders and clean up loader_data graph + ClassLoaderDataGraph::purge(true /* at_safepoint */); + DEBUG_ONLY(MetaspaceUtils::verify();) + + // Need to clear claim bits for the next mark. + ClassLoaderDataGraph::clear_claimed_marks(); } { diff --git a/src/hotspot/share/gc/serial/serialHeap.cpp b/src/hotspot/share/gc/serial/serialHeap.cpp index d6d51eef453..48d6a1b74ea 100644 --- a/src/hotspot/share/gc/serial/serialHeap.cpp +++ b/src/hotspot/share/gc/serial/serialHeap.cpp @@ -603,9 +603,6 @@ void SerialHeap::do_full_collection(bool clear_all_soft_refs) { gc_prologue(); COMPILER2_OR_JVMCI_PRESENT(DerivedPointerTable::clear()); CodeCache::on_gc_marking_cycle_start(); - ClassUnloadingContext ctx(1 /* num_nmethod_unlink_workers */, - false /* unregister_nmethods_during_purge */, - false /* lock_nmethod_free_separately */); STWGCTimer* gc_timer = SerialFullGC::gc_timer(); gc_timer->register_gc_start(); @@ -630,13 +627,6 @@ void SerialHeap::do_full_collection(bool clear_all_soft_refs) { _old_gen->compute_new_size(); _young_gen->compute_new_size(); - // Delete metaspaces for unloaded class loaders and clean up loader_data graph - ClassLoaderDataGraph::purge(/*at_safepoint*/true); - DEBUG_ONLY(MetaspaceUtils::verify();) - - // Need to clear claim bits for the next mark. - ClassLoaderDataGraph::clear_claimed_marks(); - _old_gen->update_promote_stats(); // Resize the metaspace capacity after full collections