← Back to Pull requests

gc: Stop objects awaiting finalization from triggering full collections #64029

Closed
unknown closed this pull request without merging on Oct 29, 2026 13:35 UTC
View on GitHub
unknown unknown commented on Oct 22, 2026 13:04 UTC
Author

When the GC finds dead objects with pending finalizers, it marks them through the to_finalize list so that their finalizers can run. They are garbage once those finalizers have run. That marking was counted in scanned_bytes, and therefore in promoted_bytes. As a result, code that creates many short-lived objects with finalizers (BigInt/BigFloat arithmetic is the common case) kept tripping the "large promotion rate" heuristic (promoted_bytes / heap_size > 0.15). It ended up doing a full collection about every other GC, each of which re-marks all long-lived data.

This PR does two things:

1. Bytes marked through to_finalize are no longer counted as promoted.
2. Pages that held such cells are flagged (has_finalized) so the next quick sweep visits them.

The second part is needed because those full collections were also doing real work. After the sweep, a cell that was marked only to run its finalizer is promoted like any other marked cell, and the page's has_young is cleared. On a page holding nothing else, that's harmless: at the next collection nothing on the page is marked, so the whole page is freed. On a page that also holds a live old object, though, quick sweeps skip the page, and the finalized cells were only reclaimed by full collections. With the flag, the next quick sweep frees them.

Reproducer: full collections

struct Node; l::Union{Node,Nothing}; r::Union{Node,Nothing}; end
tree(d) = d == 0 ? Node(nothing, nothing) : Node(tree(d - 1), tree(d - 1))
data = tree(20)   # some long-lived data (2M nodes)

f(n) = sum(i -> Int(big(i) * big(3) % 7), 1:n)   # BigInt temporaries, each with a finalizer
f(10); GC.gc(true)

reason() = Base.full_sweep_reasons()[:FULL_SWEEP_REASON_LARGE_PROMOTION_RATE]
a = Base.gc_num(); r = reason()
@time f(20_000_000)
b = Base.gc_num()
println("GCs: ", b.pause - a.pause, ", full GCs: ", b.full_sweep - a.full_sweep,
        ", of which due to \"large promotion rate\": ", reason() - r)

Before:

  8.318478 seconds (220.00 M allocations: 4.768 GiB, 23.63% gc time)
GCs: 83, full GCs: 42, of which due to "large promotion rate": 41

After:

  7.878030 seconds (220.00 M allocations: 4.768 GiB, 18.82% gc time)
GCs: 93, full GCs: 3, of which due to "large promotion rate": 0

These numbers were measured on top of https://github.com/JuliaLang/julia/pull/64027 (the BigInt heap-size accounting fix), so that only this change differs.

The effect is not specific to BigInt. A plain mutable struct with a finalizer, churned the same way, does one promotion-triggered full collection per round on master and none with this change. How much time is saved depends on the size of the long-lived heap, since each avoided full collection would have re-marked all of it.

Reproducer: finalized objects sharing pages with live objects

This checks that removing those full collections doesn't leave memory behind. One in 1024 finalizable objects is kept alive for a while, so live and finalized objects share pages:

mutable struct Handle; x::Int; end
@noinline release(h::Handle) = nothing
const RING = Vector{Any}(undef, 65536)
function churn(n, off)
    for i in 1:n
        h = Handle(i); finalizer(release, h)
        i % 1024 == 0 && (RING[((i + off) ÷ 1024) % length(RING) + 1] = h)
    end
end
churn(10, 0); GC.gc(true)
for k in 1:12
    churn(10_000_000, k * 10_000_000)
    GC.gc(false)
    println("round $k: pool_live_bytes after a young GC = ",
            ccall(:jl_gc_pool_live_bytes, Int64, ()) >> 20, " MB")
end

About 1 MB is actually live. Memory held after a young GC in rounds 4–12:

  • master: flat at about 153 MB, with 4 full collections

  • this PR: flat at about 153 MB, with 1 full collection
  • The roughly 153 MB in both builds is the last round's objects, whose finalizers ran after that collection; the next collection frees them.

    🤖 Generated with Claude Code

    Diff: 493 lines (+369 / -124) across 6 files Reviewer: topolarity Review duration: 10111m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 493 lines diff across 6 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.371
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #64029"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.4% (MEDIUM) · Higher = larger blast radius
    52.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.7% · Safe to merge
    20.7%