The CPython Memory Engine

Keynote
Shenzhen
4:20 p.m. - 5 p.m.
  • Petr Andreev Russian Professor · CPython Contribution Team Lead · PSF Board of Directors Nominee

    Specializes in CPython internals, optimization, and high-performance computing.

    Driven by GPU acceleration, CPU vectorization. Evolved from ML systems to CPython core research engineer.

    8+ years leading teams in AI, maths, and physics. PyCon speaker.

    Lecturer at Moscow Institute of Physics and Technology – top 1 Russian university.

    Open to talks and collaboration.

    anonymous

Abstract

Memory allocation, deallocation, and free list techniques

Details

This talk takes a hands-on look at how CPython actually manages memory — from the moment you write obj = SomeClass() all the way to the point where the memory backing that object is eventually reused, freed, or returned to the operating system.

We'll start with the fundamentals: object layout, reference counting, and how CPython's small-object allocator, pymalloc, allocates memory through arenas and pools instead of calling malloc for every tiny object you create. You'll see what CPython actually does under the hood when a hot loop creates large numbers of integers, tuples, and other short-lived objects.

Next, we'll walk through what happens at the end of an object's life: what happens once its reference count drops to zero? How does CPython run type-specific deallocation logic and optional finalizers? How does a user-defined __del__ fit into this? We'll also look at how memory gets returned to the allocator, or kept around in a free list for fast reuse — and why, from the outside, this can sometimes look like a memory leak.

In the second half, we'll focus on optimization mechanisms and swapping out the memory allocator. We'll cover the free lists used by integers, tuples, frames, and other core objects: why they exist, how they speed up hot paths, and why they can sometimes keep memory held onto longer than you'd expect. We'll then compare pymalloc and mimalloc, looking at how they differ in fragmentation and multithreaded behavior, and what actually changes when you run Python with mimalloc. Finally, we'll use simple benchmarks and tools like tracemalloc to see how to measure the real-world impact of these changes.

By the end of this talk, you'll have a clear mental model of CPython's memory allocation and deallocation pipeline, and an intuitive understanding of what free lists and other memory allocators are actually doing.