Open In App

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

Last Updated : 03 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We have two Python methods, __sizeof__() and sys.getsizeof(), both used to measure the memory size of an object. While they seem similar, they produce different results. For example, calling these methods on the same object may return different values. Understanding this difference is essential for efficient memory management especially in large-scale applications. Let’s explore how these methods work and what sets them apart.

sys.getsizeof() Method

A function from the sys module that measures an object’s size in bytes, including extra memory used by Python’s garbage collector, it calls ‘__sizeof__()’ internally but adds the garbage collector’s overhead—extra memory Python reserves to manage objects. Let’s explore it using an example:

Python
import sys
a = [1, 2]              # Small list
b = [1, 2, 3, 4]        # Medium list
d = [2, 3, 1, 4, 66, 54, 45, 89]  # Larger list

print(sys.getsizeof(a))  
print(sys.getsizeof(b))  
print(sys.getsizeof(d))  

Output
72
88
120

Explanation:

  • Base Size: In this system an empty list takes 56 bytes, covering its structure and overhead (it can vary from system to sytem).
  • Per Element: Each item adds 8 bytes for a pointer to the value giving output 72, 88 and 120.

__sizeof__() Method

Perfect for understanding an object’s true footprint, such as comparing data structures in a performance study. For instance, if you’re testing whether a list, tuple, or set is more memory-efficient for storing a dataset, __sizeof__() reveals their baseline sizes without the garbage collector’s overhead muddying the results.

Python
w = [1, 2]              # Small list
x = [4, 5, 7, 9]        # Medium list
z = [54, 45, 12, 23, 24, 90, 20, 40]  # Larger list

print(w.__sizeof__())  
print(x.__sizeof__()) 
print(z.__sizeof__())   

Output
56
72
104

Explanation:

  • Base Size: An empty list takes 40 bytes, just for its basic structure.
  • Per Element: Each item adds 8 bytes for a pointer to the value.

Difference between sys.getsizeof() and __sizeof__()

Feature

sys.getsizeof()

__sizeof__()

Module

Requires sys library

Built-in

Includes Overhead ?

Yes (e.g., 16 bytes)

No

Empty List Size

56 bytes (can vary system to system)

40 bytes (can vary system to system)

Use Case

Real-world memory use

Base object size

When To Use Each

  • sys.getsizeof(): Use this when you need to assess the total memory usage of an object in a live program. It’s great for tasks like profiling a script handling large datasets or debugging why your application is consuming more RAM than expected. For instance, it helps you see the full impact of a list, including garbage collector overhead, which is key for real-world optimization.
  • __sizeof__(): Turn to this when you want to measure an object’s core size without extra overhead, perfect for performance comparisons or algorithm tuning. It’s handy for analyzing whether a list or tuple is more memory-efficient for a specific task, like storing data in a performance-sensitive project, giving you a clear view of the object’s baseline footprint.


Next Article
Article Tags :
Practice Tags :

Similar Reads