Saturday 11:30 a.m.–noon

Garbage Collection in Python

Benjamin Peterson

Audience level:
Experienced
Category:
Python Internals

Description

This talk will explore how garbage collection is implemented in CPython and PyPy. See how CPython deals reference counting's shortcomings with a special GC for cycle collection. Then dive into PyPy's sophisticated and high-performing GC implementations. The particularly thorny issue of finalizers in reference cycles will also be addressed.

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.