what is the python object overhead for boolean
The Python object overhead for a boolean in the standard CPython implementation is primarily
due to the full Python object system structure. A bool object is a subclass of int and shares the
same internal representation, which includes:
A reference count (usually 8 bytes on a 64-bit system),
A pointer to the type object (usually 8 bytes),
Additional integer object internals.
This results in each boolean object consuming about 28 bytes of memory in total, combining the
Python object overhead plus the integer storage internals.
Key points:
The theoretical size of a bool value is just 1 bit, but Python objects carry metadata and
bookkeeping information that requires much more memory.
This overhead (~28 bytes) covers the object header and representation, not just the data.
True and False are singletons, so only two such objects exist; other references point to these
objects.
This design choice prioritizes Python’s dynamic and object-oriented features over memory
minimalism.
This has been confirmed from multiple sources such as Stack Overflow and detailed discussions
about CPython object memory layout [1] [2] [3] .
In summary:
The Python boolean object overhead is about 28 bytes due to Python's object model, including
reference count, type pointer, and integer data structures that represent the bool subtype of int.
References:
Stack Overflow: Python booleans take 24-28 bytes due to object overhead [1] [2]
cs.stackexchange.com explanation on bool size and Python object internals [3]
⁂
1. https://stackoverflow.com/questions/34502755/why-do-python-booleans-take-up-more-than-a-byte
2. https://www.reddit.com/r/Python/comments/8pguq5/why_is_a_boolean_variable_24_bytes_in_python/
3. https://cs.stackexchange.com/questions/162451/why-boolean-value-in-python-is-24-28-bytes