From 3812c7d0d3db94920df028ed5890886c073d1ade Mon Sep 17 00:00:00 2001 From: Thomas Schatzl Date: Thu, 13 Nov 2025 14:59:29 +0100 Subject: [PATCH] 8371791 Hi all, please review this change that improves the accuracy of `G1CollectedHeap::non_young_occupancy_after_allocation()` by considering the free space in the old gen retained region. Testing: tier1-3, gha Thanks, Thomas --- src/hotspot/share/gc/g1/g1Allocator.cpp | 8 ++++++++ src/hotspot/share/gc/g1/g1Allocator.hpp | 3 +++ src/hotspot/share/gc/g1/g1CollectedHeap.cpp | 4 ++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/hotspot/share/gc/g1/g1Allocator.cpp b/src/hotspot/share/gc/g1/g1Allocator.cpp index 713bafd478268..78710084ee37e 100644 --- a/src/hotspot/share/gc/g1/g1Allocator.cpp +++ b/src/hotspot/share/gc/g1/g1Allocator.cpp @@ -123,6 +123,14 @@ void G1Allocator::reuse_retained_old_region(G1EvacInfo* evacuation_info, } } +size_t G1Allocator::free_bytes_in_retained_old_region() const { + if (_retained_old_gc_alloc_region == nullptr) { + return 0; + } else { + return _retained_old_gc_alloc_region->free(); + } +} + void G1Allocator::init_gc_alloc_regions(G1EvacInfo* evacuation_info) { assert_at_safepoint_on_vm_thread(); diff --git a/src/hotspot/share/gc/g1/g1Allocator.hpp b/src/hotspot/share/gc/g1/g1Allocator.hpp index 19b19c06e9249..9a7e62f5cc6b9 100644 --- a/src/hotspot/share/gc/g1/g1Allocator.hpp +++ b/src/hotspot/share/gc/g1/g1Allocator.hpp @@ -103,7 +103,10 @@ class G1Allocator : public CHeapObj { void init_gc_alloc_regions(G1EvacInfo* evacuation_info); void release_gc_alloc_regions(G1EvacInfo* evacuation_info); void abandon_gc_alloc_regions(); + bool is_retained_old_region(G1HeapRegion* hr); + // Return the amount of free bytes in the current retained old region. + size_t free_bytes_in_retained_old_region() const; // Node index of current thread. inline uint current_node_index() const; diff --git a/src/hotspot/share/gc/g1/g1CollectedHeap.cpp b/src/hotspot/share/gc/g1/g1CollectedHeap.cpp index f04658a1415e9..d18f61ff50770 100644 --- a/src/hotspot/share/gc/g1/g1CollectedHeap.cpp +++ b/src/hotspot/share/gc/g1/g1CollectedHeap.cpp @@ -2964,8 +2964,8 @@ void G1CollectedHeap::abandon_collection_set() { } size_t G1CollectedHeap::non_young_occupancy_after_allocation(size_t allocation_word_size) { - // For simplicity, just count whole regions. - const size_t cur_occupancy = (old_regions_count() + humongous_regions_count()) * G1HeapRegion::GrainBytes; + const size_t cur_occupancy = (old_regions_count() + humongous_regions_count()) * G1HeapRegion::GrainBytes - + _allocator->free_bytes_in_retained_old_region(); // Humongous allocations will always be assigned to non-young heap, so consider // that allocation in the result as well. Otherwise the allocation will always // be in young gen, so there is no need to account it here.