Does garbage collection run in a separate thread in free threaded python?

Hi all,

Title kind of says it all. Does the GC run in a different thread in free threaded python (like it does in Java), or do you explicitly need to create a GC thread?

If it doesn’t, should it by default?

There this, cpython/InternalDocs/garbage_collector.md at main · python/cpython · GitHub
I’m hoping the GC never behaves like .NET’s collector

Thanks for the link.

Interesting. The free threaded GC isn’t generational and has 2 stop the world points…

No it runs in the same thread that invoked the GC. If it’s invoked manually via gc.collect(), then it’s whatever thread called gc.collect(). If it’s scheduled automatically, then it’s whatever thread first noticed that the GC needs to be run.

There aren’t any immediate plans to change this, but there are a few things we may consider in the future:

  • We may want to run finalizers in a separate thread in order to avoid deadlocks. There’s been some discussion about this even before the free threading effort.
  • We may want to run parts of the GC in parallel (in multiple threads). Meta’s CPython fork Cinder does this and it’s effective for large heaps, but possibly slower for small heaps.

I’m not sure I understand this part of the question. You don’t need to explicitly create a GC thread.

I meant is it like java where a thread to perform GC is automatically created?

And evidently the answer is “no”.