From 2018 to 2021, I worked on a greenfield Python runtime called Skybison. One of its major differences from CPython was that it used a moving garbag

Weak references and garbage collectors

submited by
Style Pass
2025-01-04 22:00:54

From 2018 to 2021, I worked on a greenfield Python runtime called Skybison. One of its major differences from CPython was that it used a moving garbage collector (GC). This I understood in theory—I knew that it ran when the heap filled up, knew we needed handles to update pointers in the runtime’s code, had read the Moon paper (PDF)—but the other day, I wanted to implement weak references and couldn’t immediately figure it out. Skybison thankfully has a reasonably clear implementation. So now I’m writing this post, mostly for myself, but maybe it will be useful to you as well.

In this post I’ll give a brief overview of a garbage collector, a sample “normal” object, and then show the special handling for weak references. I’ve taken inspiration from the Skybison code, but it’s possible other projects have different approaches.

While this post talks mostly about moving garbage collectors, I think the weakref handling applies pretty cleanly to mark-sweep and other types of stop-the-world GC as well. I don’t know about reference counting or concurrent GC, though.

Leave a Comment