With sys.getsizeof() you can check the memory usage of an object. To do so, first import the module sys:
import sys
mylist = range(0, 10000)
print(sys.getsizeof(mylist))
# 48Code language: Python (python)
Woah… wait… why is this huge list only 48 bytes?
It’s because range returns a lazy sequence. It behaves like a list of numbers, but internally it only stores the start, stop and step values, and calculates each number when you ask for it. A range is magnitudes more memory efficient than using an actual list of numbers.
You can see for yourself by using a list comprehension to create an actual Python list of numbers from the same range:
import sys
myreallist = [x for x in range(0, 10000)]
print(sys.getsizeof(myreallist))
# 85176Code language: Python (python)
That’s roughly 85KB for 10,000 numbers. The exact number can differ a little between Python versions.
Not very accurate
One thing to note: this method is not very accurate. sys.getsizeof will not recursively calculate the memory usage of all objects in a list or dictionary. So when requesting the size of a list, you request only the size of the list itself and all its references to the content, but the size of all those integers themselves is not taken into account. E.g., a Python integer takes up 28 bytes by itself:
>>> import sys
>>> sys.getsizeof(1)
28
>>> 10000 * 28
280000Code language: Python (python)
10K integers will take up an additional 280K or so bytes of memory, in addition to the list size of 85K. It’s a rough estimate: Python reuses the small integers from -5 to 256 instead of creating new ones, and memory is allocated in chunks. To get a real number, you need other tools.
Measure real memory use with tracemalloc
The standard library’s tracemalloc module tracks every memory allocation Python makes. Start it, run your code, and ask how much memory was allocated:
import tracemalloc
tracemalloc.start()
numbers = list(range(10_000))
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
print(f"Current: {current:,} bytes, peak: {peak:,} bytes")Code language: Python (python)
On my computer, with Python 3.15, this prints:
Current: 364,088 bytes, peak: 364,088 bytesThat’s the list and the integers together, and it’s close to our estimate: roughly 80K for the list plus 280K for the integers. The peak value is useful too: it tells you the most memory your code used at any point, even if it freed some of it later.
Get the deep size of an object with Pympler
If you want the total size of one object, including everything it refers to, use the asizeof function from the third-party Pympler package. Install it in your virtual environment with python -m pip install pympler, and compare it with sys.getsizeof:
import sys
from pympler import asizeof
numbers = list(range(10_000))
print(sys.getsizeof(numbers))
# 80056
print(asizeof.asizeof(numbers))
# 400056Code language: Python (python)
asizeof walks through the list and adds the size of every integer in it. It also works for dictionaries, your own objects, and anything nested inside them. It reports a bit more than tracemalloc here, because it rounds each integer up to 32 bytes, the amount of memory Python really reserves for it, and it also counts the small integers that Python shares between objects.