Python Memory Deep Dive for Speed and Efficiency
summary
CPython uses reference counting plus a generational GC (small objects <=512 bytes are pooled by pymalloc), so memory behavior depends on allocators and object topology rather than just types. Use sys.getsizeof and a recursive sizeutil.get_full_size to measure real cost, tune gc with gc.get_threshold / gc.set_threshold (default gen0 = 700), prefer generators over lists to cut peak RAM (demo goes ~335MB → ~124MB), reuse/overwrite locals to free memory earlier, and large workloads can drop collections (example sitemap: 77 → 2) and speed up tests.
AI-written summary · about
