Difference between __sizeof__() and getsizeof() method in Python


The __sizeof__() method and the getsizeof() method both are used to get the size of the objects used in the program. The getsizeof() method returns an additional overhead for garbage collection along with each element size of the list. The __sizeof__() method returns the actual size of the object without any overhead. In this article, we will see how we can differentiate these operators in Python.

__sizeof__() operator

getsizeof()

The __sizeof__() operator returns the size of the object without any extra overhead for garbage collection.

The getsizeof() operator returns the size of the object with extra overhead for garbage collection.

It returns 40 bytes for an empty list and 8 bytes for each additional list element.

It returns 64 bytes for an empty list and 8 bytes for each additional list element.

It is not system specific and returns the same value on any system.

It is a system specific method i.e value returned by this method depends on the type of system it is executed on.

Importing the sys module is not required for __sizeof__() function to execute.

The sys module is required before using getsizeof() method.

Using getsizeof() operator

The getsizeof() operator internally calls the __sizeof__() operator and adds an extra overhead while returning the size of the object for garbage collection. It returns 64 bytes(depending upon the system it can vary) for an empty list and 8 bytes for each list element.

Example

import sys
empty_list=[]
a =[34, 22]
b =[45, 56, 5, 60]
c =[23, 28, 38, 40]
d =[2, 3, 1, 4, 66, 54, 45, 89]
print(sys.getsizeof(empty_list))
print(sys.getsizeof(a))
print(sys.getsizeof(b))
print(sys.getsizeof(c))
print(sys.getsizeof(d))

Output

56
72 
88
88
120

Explanation

In the above example, we define a list with lengths 0,2,4,4,8. When we find the size of the list using the __sizeof__() operator the size of the empty list is returned 56. It can vary depending on the size of the system we are using. For each additional element in the list, 8 bytes are added to the size. So the list with two values returns a size of 72(56+16). Similarly lists b,c and d with 4,4 and 8 values return sizes of 88,88 and 120 bytes respectively.

Using __sizeof__() function

The __sizeof__() function returns the size of the object without any overhead for garbage collection. For an empty list, it returns a size of 40 bytes and for each additional element of the list, it adds 8 bytes to the size.

Example

w =[1, 2]
x =[4, 5, 7, 9]
y =[2, 8, 6, 56, 45, 89, 88]
z =[54, 45, 12, 23, 24, 90, 20, 40]
print(w.__sizeof__())
print(x.__sizeof__())
print(y.__sizeof__())
print(z.__sizeof__())

Output

56
72
104
104

Conclusion

In this article, we discussed the difference between the __sizeof__() function and getsizeof() method which are used to get the size of the object. The getsizeof() method internally calls __sizeof__() method and adds additional overhead bytes for garbage collection. The sizeof() function returns the size of the object without adding any additional overhead for garbage collection.

Updated on: 17-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements