Python uses reference counting as its primary garbage collection mechanism. Every object stores a count of references pointing to it and as long as the reference count of an object remains non-zero, the runtime keeps the object alive.
Reference counting is an integral part of the CPython implementation and a great way to get acquainted with its internals. In this talk we will focus on the relevant implementation details of reference counting in CPython. In particular, we will cover the following details:
- Memory layout of objects in CPython
- How reference counts are manipulated
- Looking at the implementation of the int type as an example
- How CPython Virtual Machine manipulates reference counts during instruction execution
- When reference counting doesn't work?