They are not the same thing at all.

len() queries for the number of items contained in a container. For a string that's the number of characters:

Return the length (the number of items) of an object. The argument may be a sequence (string, tuple or list) or a mapping (dictionary).

sys.getsizeof() on the other hand returns the memory size of the object:

Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.

Python string objects are not simple sequences of characters, 1 byte per character.

Specifically, the sys.getsizeof() function includes the garbage collector overhead if any:

getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.

String objects do not need to be tracked (they cannot create circular references), but string objects do need more memory than just the bytes per character. In Python 2, __sizeof__ method returns (in C code):

Py_ssize_t res;
res = PyStringObject_SIZE + PyString_GET_SIZE(v) * Py_TYPE(v)->tp_itemsize;
return PyInt_FromSsize_t(res);

where PyStringObject_SIZE is the C struct header size for the type, PyString_GET_SIZE basically is the same as len() and Py_TYPE(v)->tp_itemsize is the per-character size. In Python 2.7, for byte strings, the size per character is 1, but it's PyStringObject_SIZE that is confusing you; on my Mac that size is 37 bytes:

>>> sys.getsizeof('')
37

For unicode strings the per-character size goes up to 2 or 4 (depending on compilation options). On Python 3.3 and newer, Unicode strings take up between 1 and 4 bytes per character, depending on the contents of the string.

For containers such as dictionaries or lists that reference other objects, the memory size given covers only the memory used by the container and the pointer values used to reference those other objects. There is no straightforward method of including the memory size of the ‘contained’ objects because those same objects could have many more references elsewhere and are not necessarily owned by a single container.

The documentation states it like this:

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

If you need to calculate the memory footprint of a container and anything referenced by that container you’ll have to use some method of traversing to those contained objects and get their size; the documentation points to a recursive recipe.

Answer from Martijn Pieters on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › difference-between-__sizeof__-and-getsizeof-method-python
Difference between __sizeof__() and getsizeof() method - Python - GeeksforGeeks
July 12, 2025 - 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: ... 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))
🌐
Python
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.
Top answer
1 of 2
85

They are not the same thing at all.

len() queries for the number of items contained in a container. For a string that's the number of characters:

Return the length (the number of items) of an object. The argument may be a sequence (string, tuple or list) or a mapping (dictionary).

sys.getsizeof() on the other hand returns the memory size of the object:

Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.

Python string objects are not simple sequences of characters, 1 byte per character.

Specifically, the sys.getsizeof() function includes the garbage collector overhead if any:

getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.

String objects do not need to be tracked (they cannot create circular references), but string objects do need more memory than just the bytes per character. In Python 2, __sizeof__ method returns (in C code):

Py_ssize_t res;
res = PyStringObject_SIZE + PyString_GET_SIZE(v) * Py_TYPE(v)->tp_itemsize;
return PyInt_FromSsize_t(res);

where PyStringObject_SIZE is the C struct header size for the type, PyString_GET_SIZE basically is the same as len() and Py_TYPE(v)->tp_itemsize is the per-character size. In Python 2.7, for byte strings, the size per character is 1, but it's PyStringObject_SIZE that is confusing you; on my Mac that size is 37 bytes:

>>> sys.getsizeof('')
37

For unicode strings the per-character size goes up to 2 or 4 (depending on compilation options). On Python 3.3 and newer, Unicode strings take up between 1 and 4 bytes per character, depending on the contents of the string.

For containers such as dictionaries or lists that reference other objects, the memory size given covers only the memory used by the container and the pointer values used to reference those other objects. There is no straightforward method of including the memory size of the ‘contained’ objects because those same objects could have many more references elsewhere and are not necessarily owned by a single container.

The documentation states it like this:

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

If you need to calculate the memory footprint of a container and anything referenced by that container you’ll have to use some method of traversing to those contained objects and get their size; the documentation points to a recursive recipe.

2 of 2
2

key difference is that len() will give actual length of elements in container , Whereas sys.getsizeof() will give it's memory size which it occupy

for more information read docs of python which is available at https://docs.python.org/3/library/sys.html#module-sys

🌐
Python.org
discuss.python.org › python help
Why the size of object or array always coming same? - Python Help - Discussions on Python.org
December 26, 2023 - I am trying to implement my own Dynamic array data structure using python i did something like below now I want to check the size of my array how could i do it. import ctypes, sys class MeraList: def __init__(self): self.size = 1 # max items that can be stored self.present = 0 # number of items ...
🌐
Reddit
reddit.com › r/learnpython › size of python objects different? [real memory vs sys.getsizeof()]
r/learnpython on Reddit: Size of python objects different? [Real memory vs sys.getsizeof()]
January 11, 2015 -

Hi Pyople!

Yesterday I learned about sys.getsizeof() function and try some code. More specifically:

lst = [i for i in range(1000000000)]  # one mld numbers, creating for about a minute

When I use sys.getsizeof(lst), it returns: 8058558880. Which is correct. But when I look at my system resources in Linux Centos7 IPython (Python 3.4) I see: ipython Memory: 39592564 K Shared Mem: 5176 K - That's freaking 40GB.

I don't understand why, if a object is 8 GB in size, takes 40 KGB system memory. I tried it in list that had around 400 MB and system took 400 * 5 (approx) = 2 GB (approx)

Why is it taking 5-times more memory than it should? Or is the problem only because I tried it in iPython / Konsole? And in program it wouldn't be a problem?

🌐
Ned Batchelder
nedbatchelder.com › blog › 202002 › sysgetsizeof_is_not_what_you_want
sys.getsizeof is not what you want | Ned Batchelder
February 9, 2020 - In the case of a dictionary, “objects it refers to” includes all of the keys and values. getsizeof is only reporting on the memory occupied by the internal table the dict uses to track all the keys and values, not the size of the keys and values themselves.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › bytes › determining-the-size-of-an-object-in-python
Determining the Size of an Object in Python
September 8, 2023 - Python provides a built-in function, sys.getsizeof(), which can be used to determine the size of an object.
🌐
Python.org
discuss.python.org › python help
Strange behavior of sys.getsizeof - Python Help - Discussions on Python.org
April 29, 2025 - Hi! I expected that sys,getsizeof would return the same results in the following two scripts, but it returns different results. Why? Script 1 import sys a = [] a += [0] print(a) print(sys.getsizeof(a)) Output of script1 [0] 72 Script 2 import sys a = [] a.append(0) print(a) print(sys.getsizeof(a)) ...
🌐
Codemia
codemia.io › knowledge-hub › path › how_do_i_determine_the_size_of_an_object_in_python
How do I determine the size of an object in Python?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-find-size-of-an-object-in-python
How to find size of an object in Python? - GeeksforGeeks
July 17, 2023 - In python, the usage of sys.getsizeof() can be done to find the storage size of a particular object that occupies some space in the memory. This function returns the size of the object in bytes.
🌐
Dr. Shahin Siami
shahin.page › subarticle › Analyze-Memory-Usage-of-Numbers-in-Python
Analyze Memory Usage of Numbers in Python Using getsizeof | Dr. Shahin Siami
September 24, 2025 - import sys # Sample list of numbers numbers = [1, 255, 1024, 999999, -42, 0] # Analyze memory usage of each number for num in numbers: size = sys.getsizeof(num) print(f"Number {num} uses {size} bytes of memory")
🌐
GoShippo
goshippo.com › blog › measure-real-size-any-python-object
How to Measure the Real Size of Any Object in Python
April 14, 2025 - When you measure a size of an object, ... etc. sys.getsizeof only gives you the size of the object and their attributes, however it does not recursively add the size of sub-attributes. So I decided to fill in the gap. I wrote the helper below to recursively measure the size of a Python object (or ...
🌐
Java Guides
javaguides.net › 2024 › 12 › python-sys-getsizeof-function.html
Python sys getsizeof() Function
December 15, 2024 - The sys.getsizeof function in Python's sys module returns the size of an object in bytes. This function is useful for memory profiling and optimizing the memory usage of your programs.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-get-file-size-in-python
How to Get File Size in Python with os and pathlib | DigitalOcean
September 8, 2025 - Learn how to get file size in Python with os and pathlib. This guide covers error handling, human-readable formats, and best practices for writing robust, pr…
🌐
Codedamn
codedamn.com › news › python
How to Determine the Size of Objects in Python
July 2, 2023 - Python provides a built-in module named 'sys' which has a method called 'getsizeof()' that can be used to get the size of an object.