Skip to content

Free-threaded: pages freed by other threads are not returned while the owning thread is idle (and the default purge delay keeps them after it exits) #161999

Closed

Description

@aryanj723

Bug report

In the free-threaded build (--disable-gil), each thread allocates from its own mimalloc heaps. When thread B frees a block owned by thread A, the block goes on the page's cross-thread free list; A collects it only when A allocates from that page, runs mi_heap_collect, or exits. Empty pages of QSBR-enabled heaps are not freed at once either: _PyMem_mi_page_maybe_free() puts them on the owner's tstate->mimalloc.page_list, which only the owner processes (_PyMem_mi_heap_collect_qsbr).

So a thread that produces data for other threads and then blocks (a worker pool, a producer, an HTTP handler thread) keeps everything it ever allocated committed. gc.collect() does not help: the free-threaded GC merges other threads' delayed-free queues during its stop-the-world pause (process_delayed_frees), but nothing collects their pages.

main has the same code.

Reproducer

Save as idle_owner.py. A worker thread builds N small dicts, hands the list to the main thread and blocks. The main thread drops the list and calls gc.collect(). Then the worker wakes, allocates a little, and later exits. The script prints RssAnon above the starting value at each step.

import gc
import queue
import sys
import threading
import time

N = int(sys.argv[1]) if len(sys.argv) > 1 else 300000

def rss_anon_kb():
    with open("/proc/self/status") as f:
        for line in f:
            if line.startswith("RssAnon:"):
                return int(line.split()[1])

handoff = queue.Queue()
wake = threading.Event()
done = threading.Event()

def worker():
    handoff.put([{"k": i, "v": [i, str(i)]} for i in range(N)])
    wake.wait()
    wake.clear()
    [object() for _ in range(1000)]
    done.set()
    wake.wait()

base = rss_anon_kb()
t = threading.Thread(target=worker)
t.start()
data = handoff.get()
peak = rss_anon_kb()
del data
gc.collect()
time.sleep(1)
idle = rss_anon_kb()
wake.set()
done.wait()
gc.collect()
woke = rss_anon_kb()
wake.set()
t.join()
gc.collect()
exited = rss_anon_kb()
print("%s gil=%s N=%d: RssAnon above start (kB): holding %d, freed by main while owner idle %d,"
      " owner woke and allocated %d, owner exited %d"
      % (sys.version.split()[0], sys._is_gil_enabled(), N, peak - base, idle - base, woke - base, exited - base))

Build 3.15.2 from the release tarball, once free-threaded and once with the GIL:

tar xf Python-3.15.2.tar.xz
mkdir obj-ft obj-gil
(cd obj-ft && ../Python-3.15.2/configure -q --disable-gil && make -j"$(nproc)")
(cd obj-gil && ../Python-3.15.2/configure -q && make -j"$(nproc)")

obj-ft/python idle_owner.py 300000
MIMALLOC_PURGE_DELAY=0 obj-ft/python idle_owner.py 300000
obj-gil/python idle_owner.py 300000

Results on x86_64 Linux (glibc), RssAnon above the start, N = 300,000:

build holding freed by main, owner idle owner woke, allocated a little owner exited
3.15.2 free-threaded 121,316 kB 121,360 kB 121,320 kB 120,440 kB
3.15.2 free-threaded, MIMALLOC_PURGE_DELAY=0 120,472 kB 120,516 kB 83,308 kB 33,444 kB
3.15.2 default (GIL) 106,124 kB 2,456 kB 2,456 kB 2,452 kB

With the default options nothing comes back even after the owner exits, because the delayed purge only runs on later allocator activity. With MIMALLOC_PURGE_DELAY=0 the memory comes back only when the owner allocates again or exits, and 33 MB stays committed after exit. I have not established what holds those 33 MB; pages abandoned at thread exit before their QSBR goal is reached are one candidate.

Impact on a real application

I hit this in a long-running multi-threaded service (about 40 threads, free-threaded 3.15.2, musl, 512 MB of RAM) that serves an HTTP API. The threads that handle requests hold the slack. After 300 s of HTTP load, an in-process heap census (walking the mimalloc heaps while the world is stopped) shows:

thread committed used
request worker 5.9 MB 1.3 MB
HTTP server thread 5.1 MB 2.0 MB
task worker 1 3.2 MB 0.75 MB
task worker 2 3.1 MB 0.57 MB

Process RSS stays 15-16 MB above its pre-load level for as long as I watched (80 s after the load stopped). I did not run the same load on a GIL build; in the reproducer above the GIL build returns the memory at once.

Expected

Memory freed by any thread becomes reusable (and, with purging, returned to the OS) within a bounded time, even if the owning thread stays idle.

Possible fixes

  1. During the GC's stop-the-world pause, after process_delayed_frees(), collect every paused thread's heaps on its behalf: move cross-thread frees to the local free lists, free empty pages, and process its QSBR page_list with its own tstate. The owners are paused, and CPython already frees other threads' pages during stop-the-world (see the comment in _PyMem_mi_page_maybe_free). Rate-limit it so frequent young collections do not walk every heap each time.
  2. Let a thread collect its own heaps when it is about to block for a long time (lock/condition wait, select) if it has freed or received cross-thread frees since the last collection.
  3. Process the owner's QSBR page_list from the eval breaker (_PyMem_ProcessDelayed), which already runs on QSBR progress, not only from the mimalloc slow path. This helps owners that wake up but allocate little.
  4. On thread exit, do not leave pages abandoned only because their QSBR goal was not reached yet; hand them to the interpreter's delayed-free queue.

I have not tried a patch for this one.

Output from python -VV: Python 3.15.2 free-threading build (main, Nov 2 2026, 15:11:15) [GCC 9.4.0]

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux

Activity

  1. self-assigned this
    on Nov 14, 2026
  2. corona10 commented on Nov 14, 2026

    @corona10
    Member

    Seems like it's duplicated with #158972

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions