>>> sys.getsizeof(int)
436 #? does this mean int occupies 436 bytes .>>> sys.getsizeof(1)
12 #12 bytes for int object, is this the memory requirement.
I thought int in python is represented by 4 bytes, why is it reporting 12 bytes
Please someone explain why is it reporting 12 bytes when int uses just 4 bytes
Yes, an int
instance takes up 12 bytes on your system. Integers (like any object) have attributes, i.e. pointers to other objects, which take up additional memory space beyond that used by the object's own value. So 4 bytes for the integer's value, 4 bytes for a pointer to __class__
(otherwise, Python wouldn't know what type the object belonged to and how to start resolving attribute names that are inherited from the int
class and its parents), and another 4 for the object's reference count, which is used by the garbage collector.
The type int
occupies 436 bytes on your system, which will be pointers to the various methods and other attributes of the int
class and whatever other housekeeping information Python requires for the class. The int
class is written in C in the standard Python implementation; you could go look at the source code and see what's in there.