Why is the size of list of elements lesser than sum of actual size of elements?
Question:
I have the following code.
Why is the size of list/tuple lesser than the sum of size of individual elements?
Also why are the sizes of bool and integer 28 bytes?
list_eg = [1,2,3,4,5,"dfd",True,3.1415]
tuple_eg = (1,2,3,4,5,"dfd",True,3.1415)
print(sys.getsizeof(list_eg))
print(sys.getsizeof(tuple_eg))
128
112
print(sys.getsizeof("dfd"))
print(sys.getsizeof(3.1415))
print(5*sys.getsizeof(3))
print(sys.getsizeof(True))
52
24
140
28
print(52+
24+
140+
28)
244
Answers:
https://docs.python.org/3/library/sys.html#sys.getsizeof
Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.
I have the following code.
Why is the size of list/tuple lesser than the sum of size of individual elements?
Also why are the sizes of bool and integer 28 bytes?
list_eg = [1,2,3,4,5,"dfd",True,3.1415]
tuple_eg = (1,2,3,4,5,"dfd",True,3.1415)
print(sys.getsizeof(list_eg))
print(sys.getsizeof(tuple_eg))
128
112
print(sys.getsizeof("dfd"))
print(sys.getsizeof(3.1415))
print(5*sys.getsizeof(3))
print(sys.getsizeof(True))
52
24
140
28
print(52+
24+
140+
28)
244
https://docs.python.org/3/library/sys.html#sys.getsizeof
Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.