Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Python Land
  • Python Tutorial
  • Courses
  • Blog
  • Contact
  • Log in
Home » Tips & Tricks » Check memory usage of your Python objects
A sys.getsizeof() balance scale: a list weighs 87632 bytes, a range only 48 bytes

Check memory usage of your Python objects

By Erik van Baaren · Published December 18, 2020 · Updated November 10, 2026

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.

Table of Contents

  • 1. Not very accurate
  • 2. Measure real memory use with tracemalloc
  • 3. Get the deep size of an object with Pympler

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 bytes

That’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.

Erik van Baaren

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python! Writing good articles takes time and effort. Did you like this tutorial? You can buy him a coffee to show your appreciation.

More about Erik

Try this yourself, right now

See what a running Python holds

The same topic as an interactive lesson: short explanation, then code you run and change in your browser, with instant feedback.

Open the interactive lesson

Free to start. Certificates and AI help with Guided.

Monty, your Python learning guide, smiling and waving

Leave a Comment Cancel reply

You must be logged in to post a comment.

Subscribe to my newsletter for Python news, tips, and tricks!

Footer subscribe

  • Home
  • Python Courses
  • Privacy Policy
  • About Us
  • Contact us
©2026 Python Land - All rights reserved
Python Land is not affiliated with Python.org or the Python Software Foundation
  • Free Tutorial
    • Install Python
      • Online Python Interpreter
      • How To Open Python on Windows, Mac, Linux
    • Introduction to Python
      • The Python REPL
      • Python Variable
      • Python String
      • Python Print function
      • Python Booleans
      • For-loop and While-loop
      • Python Functions
      • Your First Python Program
      • Python Comments
    • Creating Python Programs
      • The Best Python IDE
      • Installing VSCode
      • VSCode Python Extensions
      • VSCode GUI Tour
      • Python in VSCode: Running and Debugging
    • Classes and Objects in Python
      • Python Constructor
      • Python Inheritance
    • Structure Your Project
      • Python Modules And Importing
      • Python Packages
    • Python Data Types
      • Python Integer
      • Python Float
      • Python Tuple
      • Python List
      • Python Set
      • Python Dictionary
    • Language Deep Dives
      • Python Try Except (Exceptions)
      • Python Functions: Advanced Concepts
      • List Comprehension
      • Python Iterator
      • Python Range
      • Python Docstrings
      • Python pass (Do Nothing)
    • Interaction with the OS
      • Python Read And Write File
      • Python Subprocess: Run External Commands
    • Venvs / Package Management
      • Working With Python’s venv
      • Pip install: the Python package manager
      • Python Poetry: Package and venv Management Made Easy
      • Python Pipenv: Another Package Manager
      • Pipx: Safely Install Packages Globally
    • Python Concurrency
      • The Python GIL (Global Interpreter Lock)
      • Setting the Baseline
      • Python Threading
      • Python Multiprocessing
    • Data Processing with Python
      • Working With JSON
        • JMESPath Python: JSON Query Language
      • Python YAML: How to Load, Read, and Write YAML
      • Python CSV: Read And Write CSV Files
    • Migrating From Python 2 to 3
      • Check Python Version On The Command-Line
      • How to Migrate To Python 3
      • Python 3 Advantages
    • Using The Unix Shell
      • Basic Linux Commands
      • Bash Scripts
      • Using the Find Command in Linux
      • Unix Pipes
      • Process and Highlight JSON with jq
      • Using the Bash History
      • Bash Multiprocessing
    • Deployment
      • PyInstaller: Create An Executable From Python Code
      • How To Use Docker To Containerize Your Python Project
      • Automatically Build and Deploy Your Python Application with CI/CD
      • Guerrilla Scale Your Web Application
    • Data Science
      • Jupyter Notebook
      • NumPy: Getting Started Tutorial
    • Python Learning Resources
  • Python Courses
  • Blog
  • Login / Register