Python's 7x Memory Cost for Integers

A Python integer object typically consumes around 28 bytes, approximately seven times more memory than a 4-byte C integer, due to metadata like reference counts and type information.

The top 3

  1. Python vs. C: Integer Memory Footprint: A Python integer (e.g., `1`) consumes 28 bytes on a 64-bit system, in stark contrast to a C `int` which typically uses only 4 bytes.
  2. Python's Object Overhead: The Root Cause: The higher memory cost in Python stems from its 'everything is an object' design, where each integer carries metadata like reference counts (8 bytes), type pointers (8 bytes), and size information (8 bytes), plus the actual value.
  3. NumPy's Memory Solution: Packed C Arrays: NumPy achieves significant memory efficiency by storing numbers in contiguous, type-specific C arrays, bypassing Python's individual object overhead and allowing for optimized data types like `int8` or `float32` to drastically reduce footprint.

Sources

Open the full topic