core.py

Episode 16: Memory Allocation

5 snips
Oct 29, 2024
Discover how Python expertly manages memory with an in-depth look at the PyMalloc allocator. The hosts explore the intricacies of memory allocation, sharing insights on memory pools and fragmentation. Learn about powerful tools like TraceMalloc and Memray that help debug memory issues. They also discuss unique challenges posed by recent macOS updates and advancements in Python 3.13 that improve memory tracing. With humor and relatable metaphors, the conversation demystifies the complex world of memory management in programming.
Ask episode
AI Snips
Chapters
Transcript
Episode notes
INSIGHT

Why Allocators Sit Above The OS

  • Memory allocation ultimately relies on OS primitives like mmap and brk which behave differently and have trade-offs.
  • High-level allocators sit above those primitives to optimize speed, fragmentation, and debugging.
INSIGHT

How PyMalloc Structures Memory

  • PyMalloc handles small Python allocations (≤512B) with its own arenas, pools, and blocks to be faster and reduce fragmentation.
  • It allocates arenas (256KB) then pools (4KB) containing fixed-size blocks to enable fast, size-class-specific allocation.
INSIGHT

Freeing Costs More Than Allocating

  • Freeing memory is more costly than allocation in PyMalloc due to bookkeeping to find pools and possibly free arenas.
  • That bookkeeping can be complex and historically relied on unsafe address-range checks compiled out for sanitizers.
Get the Snipd Podcast app to discover more snips from this episode
Get the app