PyCon Portugal 2026

strong ref, weakref and garbage collector walks into a bar

In Python, objects live as long as something is holding on to them—and sometimes that’s way longer than you expect. This talk explores how references, the garbage collector, and weak references interact to determine object lifetimes, with real-world examples like lru_cache quietly keeping objects alive.


This talk explores how Python manages object lifetimes through references, and why objects sometimes remain in memory longer than expected. While Python’s memory model is often described as simple, its real behavior — especially in the presence of caching and reference cycles — can lead to surprising and hard-to-debug issues.

The talk is structured as a narrative in three parts: strong references, weak references, and the garbage collector. First, we take a look at Python’s object model and reference counting, using simple, visual examples to demonstrate how references work. Next, we introduce weak references and show how they differ from regular references, including practical use cases and common misconceptions. Finally, we examine the garbage collector and explain why it is needed in Python on top of reference counting.

The concepts are grounded in a real-world debugging story involving functools.lru_cache, where a small optimization led to unexpected memory growth.

The goal of this talk is to provide a clear and practical mental model of Python’s memory behavior, helping developers understand not just how things work, but why seemingly correct code can lead to subtle memory issues.


Audience Level: Intermediate What are the main topics of your talk?:

strong ref, weakref and garbage collector