Skip to content

Free-threaded GC: Looking at reducing cost of gc.collect() cost after a gc.freeze() #162511

Backlog

Description

@johng

In the free-threaded build, each gc.collect() starts with a fast "mark-alive" pass. It marks every object reachable from known roots (module globals, thread stacks) as alive, so the slower cycle analysis that follows only has to examine what is left. On large or churned heaps this makes collections much faster.

Once gc.freeze() has been called, however, the mark-alive pass is disabled for the rest of the process. This looks to be to protect the common pattern of calling gc.freeze() before os.fork(): the pass writes an alive flag to each object it visits, and writing to an object inherited by a child process causes its memory page to be copied into the child instead of staying shared with the parent.

Disabling the pass can have quite a negative effect on garbage collection, depending on the heap. The table below compares gc.collect() time against the same heap never frozen, and shows a significant cost, especially when the heap grows after the gc.freeze():

Frozen share inst (fresh) jsondoc jsonaged (churned) aged (churned)
Never frozen (baseline) 54.8 ms 66.6 ms 137.9 ms 261.9 ms
~1.5–3.4% (freeze after imports) 1.77x 1.67x 3.07x 2.47x
10% 1.62x 1.52x 2.74x 2.20x
20% 1.53x 1.42x 2.47x 1.97x
30% 1.41x 1.28x 2.10x 1.76x
45% 1.21x 1.09x 1.76x 1.42x
70% 0.89x 0.75x 0.96x 0.86x
90% (pre-fork pattern) 0.66x 0.53x 0.40x 0.44x

I wanted to use this issue to discuss potential approaches to reducing that cost. The one I will suggest is to keep the mark-alive pass enabled and add the frozen objects as roots for it. Frozen objects are never collected by the cyclic GC, so everything they reference is alive.

The pass reads each frozen object to find its references but never marks or writes to it, which keeps the copy-on-write benefit. This adds the overhead of finding and reading the frozen objects, but it reduces the overall cost in most scenarios:

Frozen share inst main inst frozen roots jsondoc main jsondoc frozen roots jsonaged main jsonaged frozen roots aged main aged frozen roots
Never frozen 1.00x 1.05x 1.00x 1.02x 1.00x 1.00x 1.00x 1.01x
~1.5–3.4% (freeze after imports) 1.77x 1.35x 1.67x 1.21x 3.07x 1.09x 2.47x 1.13x
10% 1.62x 1.29x 1.52x 1.17x 2.74x 1.13x 2.20x 1.13x
20% 1.53x 1.32x 1.42x 1.18x 2.47x 1.20x 1.97x 1.17x
30% 1.41x 1.32x 1.28x 1.17x 2.10x 1.26x 1.76x 1.21x
45% 1.21x 1.35x 1.09x 1.19x 1.76x 1.41x 1.42x 1.27x
70% 0.89x 1.36x 0.75x 1.16x 0.96x 1.54x 0.86x 1.38x
90% (pre-fork pattern) 0.66x 1.41x 0.53x 1.16x 0.40x 1.71x 0.44x 1.48x

As you can see, there is a cut-off beyond which this approach does worse than main. This comes naturally: as more of the heap is frozen, main's slow analysis has less to do.

Activity

  1. added
    interpreter-core
    performance
    type-feature
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coreperformancetopic-free-threadingtype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions