Skip to content

Commit 9ede9a2

Browse files
authored
fix for excessive gen0 in high memory load situation (#61884)
in high memory load situations, when we had to wait for a BGC to finish, we switched to preemptive mode which means GCs could have occurred and replenished the budget. but when we come back from the wait, we use the previous decision on whether the budget was exceeded or not. this causes us to trigger GC incorrect and you would see GCs triggered when the allocated bytes are tiny.
1 parent 574b2d4 commit 9ede9a2

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/coreclr/gc/gc.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7151,7 +7151,6 @@ void gc_mechanisms::init_mechanisms()
71517151
found_finalizers = FALSE;
71527152
#ifdef BACKGROUND_GC
71537153
background_p = gc_heap::background_running_p() != FALSE;
7154-
allocations_allowed = TRUE;
71557154
#endif //BACKGROUND_GC
71567155

71577156
entry_memory_load = 0;
@@ -7444,15 +7443,6 @@ void gc_heap::reset_allocation_pointers (generation* gen, uint8_t* start)
74447443

74457444
bool gc_heap::new_allocation_allowed (int gen_number)
74467445
{
7447-
#ifdef BACKGROUND_GC
7448-
//TODO BACKGROUND_GC this is for test only
7449-
if (!settings.allocations_allowed)
7450-
{
7451-
dprintf (2, ("new allocation not allowed"));
7452-
return FALSE;
7453-
}
7454-
#endif //BACKGROUND_GC
7455-
74567446
if (dd_new_allocation (dynamic_data_of (gen_number)) < 0)
74577447
{
74587448
if (gen_number != 0)
@@ -16084,18 +16074,22 @@ void gc_heap::wait_for_background (alloc_wait_reason awr, bool loh_p)
1608416074
add_saved_spinlock_info (loh_p, me_acquire, mt_wait_bgc);
1608516075
}
1608616076

16087-
void gc_heap::wait_for_bgc_high_memory (alloc_wait_reason awr, bool loh_p)
16077+
bool gc_heap::wait_for_bgc_high_memory (alloc_wait_reason awr, bool loh_p)
1608816078
{
16079+
bool wait_p = false;
1608916080
if (gc_heap::background_running_p())
1609016081
{
1609116082
uint32_t memory_load;
1609216083
get_memory_info (&memory_load);
1609316084
if (memory_load >= m_high_memory_load_th)
1609416085
{
16086+
wait_p = true;
1609516087
dprintf (GTC_LOG, ("high mem - wait for BGC to finish, wait reason: %d", awr));
1609616088
wait_for_background (awr, loh_p);
1609716089
}
1609816090
}
16091+
16092+
return wait_p;
1609916093
}
1610016094

1610116095
#endif //BACKGROUND_GC
@@ -17203,18 +17197,27 @@ allocation_state gc_heap::try_allocate_more_space (alloc_context* acontext, size
1720317197
}
1720417198

1720517199
#ifdef BACKGROUND_GC
17206-
wait_for_bgc_high_memory (awr_gen0_alloc, loh_p);
17200+
bool recheck_p = wait_for_bgc_high_memory (awr_gen0_alloc, loh_p);
1720717201
#endif //BACKGROUND_GC
1720817202

1720917203
#ifdef SYNCHRONIZATION_STATS
1721017204
bad_suspension++;
1721117205
#endif //SYNCHRONIZATION_STATS
1721217206
dprintf (2, ("h%d running out of budget on gen%d, gc", heap_number, gen_number));
1721317207

17214-
if (!settings.concurrent || (gen_number == 0))
17208+
#ifdef BACKGROUND_GC
17209+
bool trigger_gc_p = true;
17210+
if (recheck_p)
17211+
trigger_gc_p = !(new_allocation_allowed (gen_number));
17212+
17213+
if (trigger_gc_p)
17214+
#endif //BACKGROUND_GC
1721517215
{
17216-
trigger_gc_for_alloc (0, ((gen_number == 0) ? reason_alloc_soh : reason_alloc_loh),
17217-
msl, loh_p, mt_try_budget);
17216+
if (!settings.concurrent || (gen_number == 0))
17217+
{
17218+
trigger_gc_for_alloc (0, ((gen_number == 0) ? reason_alloc_soh : reason_alloc_loh),
17219+
msl, loh_p, mt_try_budget);
17220+
}
1721817221
}
1721917222
}
1722017223
}

src/coreclr/gc/gcpriv.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@ class gc_mechanisms
504504
#ifdef BACKGROUND_GC
505505
BOOL background_p;
506506
bgc_state b_state;
507-
BOOL allocations_allowed;
508507
#endif //BACKGROUND_GC
509508

510509
#ifdef STRESS_HEAP
@@ -1791,7 +1790,7 @@ class gc_heap
17911790
void wait_for_background (alloc_wait_reason awr, bool loh_p);
17921791

17931792
PER_HEAP
1794-
void wait_for_bgc_high_memory (alloc_wait_reason awr, bool loh_p);
1793+
bool wait_for_bgc_high_memory (alloc_wait_reason awr, bool loh_p);
17951794

17961795
PER_HEAP
17971796
void bgc_uoh_alloc_clr (uint8_t* alloc_start,

0 commit comments

Comments
 (0)