PyCon India 2025

Memory Management in Python: Foundations, Problems, and Modern Techniques
2025-09-13 , Track 3

Memory management in Python is mostly automatic - but not invisible. From reference cycles and unexpected leaks to costly object growth and GC pauses, memory issues still affect real-world Python applications. Understanding how Python allocates, tracks, and reclaims memory is key to writing efficient and predictable programs - especially for long-running services, data-heavy scripts, or performance-sensitive tools.

This talk starts from first principles - how reference counting and garbage collection work - then builds up to real-world issues like circular references, hidden object retention, and finalizer behavior. We’ll explore modern tools like tracemalloc and pympler for memory diagnostics, and cover practical techniques like using slots, generators, and object reuse patterns. A live demo will walk through detecting and fixing a subtle memory leak. The goal is to make memory behavior in Python understandable, measurable, and improvable.


  1. Introduction: Why Memory Management Still Matters (2 min)
    - Python automates memory - but hidden issues still surface
    - Leaks and growth affect performance, stability, and cost

  2. Foundations: How Does Python Track Objects? (3 min)
    - Reference counting and basic garbage collection
    - Why some objects don’t get freed automatically

  3. Common Problems: Where Does Memory Go Wrong? (4 min)
    - Circular references, long-lived containers, and object retention
    - Leaks from closures, caches, and global state

  4. Under the Hood: What Else Affects Object Cleanup? (3 min)
    - Finalizers (__del__) and their caveats
    - Generational GC and collection thresholds

  5. Modern Tools: How to See What’s Happening in Memory? (3 min)
    - Using tracemalloc to trace allocations and diffs
    - Inspecting live objects with pympler and gc module

  6. Practical Techniques: How to Reduce Memory Footprint? (3 min)
    - Use of generators and streaming
    - Saving memory with __slots__, object reuse, and data structures

  7. Demo: Can We Catch a Real Memory Leak? (4 min)
    - Walkthrough: small script with growing memory
    - Fixing the issue using diagnostic tools

  • Q&A (3 min)

Prerequisites

Comfort with Python basics

Target Audience

Intermediate

Jithu is an Associate Product Architect at KeyValue Software Systems, based in Kerala. He works primarily on backend systems, with experience in both enterprise environments and freelance projects. With experience spanning ERP systems to modern cloud-native applications, he focuses on building scalable, maintainable systems using Python, Go, and related technologies. Jithu values clean architecture, practical design, and developer ergonomics.