Pycon Israel 2021

Avoiding memory leaks with "weakref"
05-02, 14:00–14:25 (Asia/Jerusalem), 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

Reuven Lerner is a full-time Python trainer. In a given year, he teaches courses at companies in the United States, Europe, Israel, India, and China — as well as to people around the world, via his online courses, including Weekly Python Exercise.

Reuven’s most recent book is “Python Workout,” a collection of Python exercises with extensive explanations, published by Manning. He is currently working on "Pandas Workout," a similar collection of exercises for Pandas.

Reuven’s free, weekly Better developers newsletter, about Python and software engineering, is read by more than 20,000 developers around the globe. His “Trainer weekly” newsletter is similarly popular among people who give corporate training.

Reuven has a bachelor’s degree in computer science and engineering from MIT, and a PhD in learning sciences from Northwestern University. He lives in Modi’in, Israel with his wife and three children.

This speaker also appears in: