From d18a0b25ccf8135951886c4d25304d3592708d64 Mon Sep 17 00:00:00 2001 From: zyf_113 <2410426332@qq.com> Date: Tue, 23 Sep 2025 14:40:40 +0800 Subject: [PATCH 1/2] fix zgc --- .../share/gc/z/zRelocationSetSelector.cpp | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/hotspot/share/gc/z/zRelocationSetSelector.cpp b/src/hotspot/share/gc/z/zRelocationSetSelector.cpp index 1475af5e869..db846c92c0e 100644 --- a/src/hotspot/share/gc/z/zRelocationSetSelector.cpp +++ b/src/hotspot/share/gc/z/zRelocationSetSelector.cpp @@ -132,7 +132,6 @@ void ZRelocationSetSelectorGroup::select_inner() { ZPage* const page = _live_pages.at(from - 1); const size_t page_live_bytes = page->live_bytes(); from_live_bytes += page_live_bytes; - from_forwarding_entries += ZForwarding::nentries(page); // Calculate the maximum number of pages needed by the candidate relocation set. // By subtracting the object size limit from the pages size we get the maximum @@ -150,15 +149,44 @@ void ZRelocationSetSelectorGroup::select_inner() { if (diff_reclaimable > _fragmentation_limit) { selected_from = from; selected_to = to; - selected_live_bytes[untype(page->age())] += page_live_bytes; - npages_selected[untype(page->age())] += 1; + from_live_bytes = 0; + } + } + + int save_selected_from = selected_from; + + from_live_bytes = 0; + selected_from = 0; + selected_to = 0; + + // Collect statistics for selected pages + for (int from = 1; from <= npages; from++) { + ZPage* const page = _live_pages.at(from - 1); + const size_t page_live_bytes = page->live_bytes(); + from_live_bytes += page_live_bytes; + from_forwarding_entries += ZForwarding::nentries(page); + + const int to = ceil((double)(from_live_bytes) / (double)(_page_size - _object_size_limit)); + const int diff_from = from - selected_from; + const int diff_to = to - selected_to; + const double diff_reclaimable = 100 - percent_of(diff_to, diff_from); + + if (from <= save_selected_from) { + selected_live_bytes[static_cast(page->age())] += page_live_bytes; + npages_selected[static_cast(page->age())] += 1; + } + + if (diff_reclaimable > _fragmentation_limit) { + selected_from = from; + selected_to = to; selected_forwarding_entries = from_forwarding_entries; + from_live_bytes = 0; } log_trace(gc, reloc)("Candidate Relocation Set (%s Pages): %d->%d, " "%.1f%% relative defragmentation, %zu forwarding entries, %s, live %d", _name, from, to, diff_reclaimable, from_forwarding_entries, - (selected_from == from) ? "Selected" : "Rejected", + (save_selected_from >= from) ? "Selected" : "Rejected", int(page_live_bytes * 100 / page->size())); } -- 2.34.1 From bc770035bc5e71b9e18c4de2af146b1b7c150ff6 Mon Sep 17 00:00:00 2001 From: zyf_113 <2410426332@qq.com> Date: Tue, 23 Sep 2025 14:49:15 +0800 Subject: [PATCH 2/2] fix zgc select relocationset --- src/hotspot/share/gc/z/zRelocationSetSelector.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/hotspot/share/gc/z/zRelocationSetSelector.cpp b/src/hotspot/share/gc/z/zRelocationSetSelector.cpp index db846c92c0e..09019f97238 100644 --- a/src/hotspot/share/gc/z/zRelocationSetSelector.cpp +++ b/src/hotspot/share/gc/z/zRelocationSetSelector.cpp @@ -149,7 +149,6 @@ void ZRelocationSetSelectorGroup::select_inner() { if (diff_reclaimable > _fragmentation_limit) { selected_from = from; selected_to = to; - from_live_bytes = 0; } } @@ -180,7 +179,6 @@ void ZRelocationSetSelectorGroup::select_inner() { selected_from = from; selected_to = to; selected_forwarding_entries = from_forwarding_entries; - from_live_bytes = 0; } log_trace(gc, reloc)("Candidate Relocation Set (%s Pages): %d->%d, " -- 2.34.1