Skip to content

Commit 8c97995

Browse files
authored
fix gc collection interval logic (#43136)
We had previously prevented full collections for the first collection, forgotten to reset counters for gc_sweep_always_full, and caused resets of other counter heuristics even when not heuristically driven.
1 parent 04d9229 commit 8c97995

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

src/gc.c

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,13 +3102,11 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
31023102
if (!prev_sweep_full)
31033103
promoted_bytes += perm_scanned_bytes - last_perm_scanned_bytes;
31043104
// 5. next collection decision
3105-
int not_freed_enough = estimate_freed < (7*(actual_allocd/10));
3105+
int not_freed_enough = (collection == JL_GC_AUTO) && estimate_freed < (7*(actual_allocd/10));
31063106
int nptr = 0;
31073107
for (int i = 0;i < jl_n_threads;i++)
31083108
nptr += jl_all_tls_states[i]->heap.remset_nptr;
31093109
int large_frontier = nptr*sizeof(void*) >= default_collect_interval; // many pointers in the intergen frontier => "quick" mark is not quick
3110-
int sweep_full;
3111-
int recollect = 0;
31123110
// trigger a full collection if the number of live bytes doubles since the last full
31133111
// collection and then remains at least that high for a while.
31143112
if (grown_heap_age == 0) {
@@ -3118,36 +3116,48 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
31183116
else if (live_bytes >= last_live_bytes) {
31193117
grown_heap_age++;
31203118
}
3121-
if (collection == JL_GC_INCREMENTAL) {
3122-
sweep_full = 0;
3123-
} else if ((collection == JL_GC_FULL || large_frontier ||
3119+
int sweep_full = 0;
3120+
int recollect = 0;
3121+
if ((large_frontier ||
31243122
((not_freed_enough || promoted_bytes >= gc_num.interval) &&
31253123
(promoted_bytes >= default_collect_interval || prev_sweep_full)) ||
3126-
grown_heap_age > 1) &&
3127-
gc_num.pause > 1) {
3128-
recollect = (collection == JL_GC_FULL);
3129-
if (large_frontier)
3130-
gc_num.interval = last_long_collect_interval;
3131-
if (not_freed_enough || large_frontier) {
3132-
if (gc_num.interval <= 2*(max_collect_interval/5)) {
3133-
gc_num.interval = 5 * (gc_num.interval / 2);
3124+
grown_heap_age > 1) && gc_num.pause > 1) {
3125+
sweep_full = 1;
3126+
}
3127+
// update heuristics only if this GC was automatically triggered
3128+
if (collection == JL_GC_AUTO) {
3129+
if (sweep_full) {
3130+
if (large_frontier)
3131+
gc_num.interval = last_long_collect_interval;
3132+
if (not_freed_enough || large_frontier) {
3133+
if (gc_num.interval <= 2*(max_collect_interval/5)) {
3134+
gc_num.interval = 5 * (gc_num.interval / 2);
3135+
}
31343136
}
3137+
last_long_collect_interval = gc_num.interval;
3138+
}
3139+
else {
3140+
// reset interval to default, or at least half of live_bytes
3141+
int64_t half = live_bytes/2;
3142+
if (default_collect_interval < half && half <= max_collect_interval)
3143+
gc_num.interval = half;
3144+
else
3145+
gc_num.interval = default_collect_interval;
31353146
}
3136-
last_long_collect_interval = gc_num.interval;
3147+
}
3148+
if (gc_sweep_always_full) {
31373149
sweep_full = 1;
3138-
promoted_bytes = 0;
31393150
}
3140-
else {
3141-
// reset interval to default, or at least half of live_bytes
3142-
int64_t half = live_bytes/2;
3143-
if (default_collect_interval < half && half <= max_collect_interval)
3144-
gc_num.interval = half;
3145-
else
3146-
gc_num.interval = default_collect_interval;
3147-
sweep_full = gc_sweep_always_full;
3151+
if (collection == JL_GC_FULL) {
3152+
sweep_full = 1;
3153+
recollect = 1;
31483154
}
3149-
if (sweep_full)
3155+
if (sweep_full) {
3156+
// these are the difference between the number of gc-perm bytes scanned
3157+
// on the first collection after sweep_full, and the current scan
31503158
perm_scanned_bytes = 0;
3159+
promoted_bytes = 0;
3160+
}
31513161
scanned_bytes = 0;
31523162
// 5. start sweeping
31533163
JL_PROBE_GC_SWEEP_BEGIN(sweep_full);

0 commit comments

Comments
 (0)