Difference between sizeof() and getsizeof() method Python (original) (raw)

Last Updated : 12 Jul, 2025

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))

`

**Explanation:

__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())

`

**Explanation:

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