Python `int` vs. C `int`: Up to 7x Memory Cost
A standard Python `int` object (e.g., value 0 or 1) typically consumes around 28 bytes on a 64-bit system, significantly more than a C `int` which usually uses just 4 bytes, due to Python's object overhead.
The top 3
- Python `int`'s Fixed Object Overhead Explained: Every Python integer, even a small number like 1, is a full-fledged object carrying a fixed overhead of approximately 28 bytes on a 64-bit system, which includes a reference count, type pointer, and size information, fundamentally differing from C's direct 4-byte value storage.
- Memory Scaling: How Large Integers Grow in Python: While Python reuses pre-allocated objects for small integers (-5 to 256), larger integers incur the base 28-byte overhead plus additional memory for their value, growing in 4-byte increments as the number of digits in base 2^30 increases; for example, 2^30 requires 32 bytes.
- List Impact: Python `int`s Lead to Multi-Megabyte Collections: A Python list of one million integers can consume hundreds of megabytes, as each list entry stores an 8-byte pointer to a separate 28-byte integer object, drastically contrasting with a C array of one million `int`s that would use only about 4 megabytes for direct value storage.
Sources
Open the full topic