Python Object Header: Minimum 24 Bytes Overhead

Every Python object, including primitive types, incurs a baseline memory overhead of at least 24 bytes (on 64-bit systems) for its object header, containing vital fields like reference count and type pointer, facilitating Python's dynamic nature.

The top 3

  1. Python Integer's 24-Byte Overhead Breakdown: A typical Python integer object, even for small values, incurs approximately 24 bytes of overhead. This includes the `ob_refcnt` (reference count) and `ob_type` (type pointer) fields, plus the internal storage for the integer value itself, making it significantly larger than a raw C integer.
  2. Fixed Overhead's Disproportionate Impact on Small Objects: Python's uniform object header leads to a disproportionately large memory footprint for small objects like single booleans, short strings, or integers, where the overhead can be many times larger than the actual data being stored.
  3. C Structs vs. Python Objects: Memory Footprint: Unlike Python objects which carry a universal header for metadata, C structs allocate only the memory strictly required for their defined fields, often with padding for alignment, resulting in significantly smaller memory footprints for raw data storage.

Sources

Open the full topic