Saturday 11:30 a.m.–noon
Garbage Collection in Python
Benjamin Peterson
- Audience level:
- Experienced
- Category:
- Python Internals
Description
Abstract
Python, like any self-respecting high-level language, automatically deallocates memory programs no longer use. In CPython the primary mechanism for this is reference counting, which C code in the interpreter implements manually. However, reference counting must be supplemented with a real GC to deal with reference cycles. In contrast to CPython's hardcoded use of reference counting, PyPy has a flexible architecture that allows for pluggable GC implementations. Its current default GC is optimized for the creation of many small, short-lived objects, a typical allocation pattern in Python. Common to both CPython and PyPy is the problem of running finalizers of objects in cycles. PyPy and very recent CPython versions have sophisticated handling of this tricky case.