Pycon Israel 2021

Avoiding memory leaks with "weakref"
2021-05-02, 14:00–14:25, General Track 1

In Python, we normally don't worry about memory usage. But that doesn't mean memory leaks are impossible! In this talk, I'll introduce "weak references" -- how they work, when you would use them, and tricks to get the most out of them.


One of the great things about Python is that it includes garbage collection. You don't have to allocate or free memory; just let the system take care of things on its own! In theory, that means you can never experience memory leaks. But in practice, that's not quite true: There are definitely ways in which you can accidentally hold onto object references, resulting in a memory leak.

Fortunately, Python provides us with "weak references" in the standard library's "weakref" module. In this talk, I'll describe Python's garbage collector, and how we can end up with memory problems despite it. I'll then show you how the "weakref" module can help us -- both on its own, and with the data structures and functionality that the "weakref" module provides.

Even if you don't need weak references, knowing how they work can give you great insight into Python's internals, and how you can take advantage of them in your work.


Session language – English Target audience – Developers, Users