TLDR Dev · ARTICLES & TUTORIALS

HOW OFTEN DOES PYTHON ALLOCATE?

zackoverflow.dev11 min read

Summary from the TLDR Dev newsletter

This dev modified CPython to track allocations and discovered that Python frequently allocates `PyLongObject` objects, especially when printing integers. However, optimizations like freelists and pool allocators help reduce the real-world impact by reusing allocated memory. While Python allocates often, the cost is mitigated by these strategies, though tagged pointers, a technique used by other languages to avoid allocation, could further improve performance.