Skip to content

GH-148937: fix for free-threaded GC (RSS based defer)#148940

Open
nascheme wants to merge 3 commits intopython:mainfrom
nascheme:ft-gc-threshold-fix-main
Open

GH-148937: fix for free-threaded GC (RSS based defer)#148940
nascheme wants to merge 3 commits intopython:mainfrom
nascheme:ft-gc-threshold-fix-main

Conversation

@nascheme
Copy link
Copy Markdown
Member

@nascheme nascheme commented Apr 23, 2026

Asking the OS for the process memory usage doesn't work well given how mimalloc works. It does not promptly return memory to the OS and so the memory doesn't drop after cyclic trash is freed.

Instead of asking the OS, use mimalloc APIs to compute how much memory is being used by all mimalloc arenas. We need to stop-the-world to do this but usually we can avoid doing a collection. So, from a performance perspective, this is worth it.

Tim Peters has a GC stress tester that quickly shows the issue, linked below. Before this fix, when I run this, the process RSS quickly goes up to 1 GB. After the fix, the RSS stays at about 100 MB. For comparision, the 3.13 GC keeps RSS at about 200 MB.

tim-gc-test.py

Asking the OS for the process memory usage doesn't work will given how
mimalloc works.  It does not promptly return memory to the OS and so the
memory doesn't drop after cyclic trash is freed.

Instead of asking the OS, use mimalloc APIs to compute how much memory
is being used by all mimalloc arenas.  We need to stop-the-world to do
this but usually we can avoid doing a collection.  So, from a
performance perspective, this is worth it.
@nascheme
Copy link
Copy Markdown
Member Author

Note that this adds two extra stop/start-the-world points. We need STW to call the mimalloc APIs to compute the memory usage (iterating through arenas). We could likely consolidate one or both of these with existing STW points but I think it makes the code more complex. So I decided to keep it simple for now. I think we should backport this change to 3.14.

It's probably better to call this inside of gc_collect_main().  That
way, we are not doing the STW from inside _PyObject_GC_Link() function.
This should have no significant performance impact since we hit this
only after the young object count hits the threshold.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant