How Do I Get The Exact Size Of A Python Object With Examples

Python is widely adopted across diverse domains, from web development to data analysis. However, in scenarios where memory optimization is crucial, understanding the memory footprint of objects becomes imperative. As Python manages memory dynamically, developers often seek to determine the exact size of various data structures.

In this guide, we will explore multiple techniques ( sys.getsizeof() and Pympler ) to precisely measure the size of Python objects, providing a comprehensive understanding of memory usage in Python.

You can learn how to use the sys.getsizeof() and the Pympler library to get Python object size and what the difference between them is. You can also know which method is more accurate to get the Python object size.

1. The sys.getsizeof() Function.

  1. To retrieve the size of a Python object, the `sys` module’s `getsizeof()` function proves to be a valuable asset.
  2. This function returns the size of an object in bytes, including the overhead.
  3. It should be noted that the size returned may not always reflect the complete memory footprint due to the complex nature of Python’s memory management.
  4. Let’s dive into some illustrative examples to grasp the concept more effectively.

1.1 Example 1: Calculating the Size of Basic Data Types.

  1. Example source code.
    import sys
    
    # Integers
    int_size = sys.getsizeof(10)
    print(f"Size of integer 10: {int_size} bytes")
    
    # Floats
    float_size = sys.getsizeof(10.5)
    print(f"Size of float 10.5: {float_size} bytes")
    
    # Strings
    str_size = sys.getsizeof("Hello, Python!")
    print(f"Size of string 'Hello, Python!': {str_size} bytes")
    
  2. Below is the above Python source code execution output.
    Size of integer 10: 28 bytes
    
    Size of float 10.5: 24 bytes
    
    Size of string 'Hello, Python!': 63 bytes

1.2 Example 2: Measuring the Size of Lists and Dictionaries.

  1. Example source code.
    import sys
    
    # Lists
    empty_list = []
    populated_list = [1, 2, 3, 4, 5]
    
    print(f"Size of empty list: {sys.getsizeof(empty_list)} bytes")
    print(f"Size of populated list: {sys.getsizeof(populated_list)} bytes")
    
    # Dictionaries
    empty_dict = {}
    populated_dict = {'a': 1, 'b': 2, 'c': 3}
    
    print(f"Size of empty dictionary: {sys.getsizeof(empty_dict)} bytes")
    print(f"Size of populated dictionary: {sys.getsizeof(populated_dict)} bytes")
    
  2. Output.
    Size of empty list: 56 bytes
    
    Size of populated list: 104 bytes
    
    Size of empty dictionary: 64 bytes
    
    Size of populated dictionary: 184 bytes

1.3 Example 3: Understanding the Complexity of Nested Structures.

  1. Example source code.
    import sys
    
    nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    nested_dict = {'outer': {'inner1': 1, 'inner2': 2}, 'outer2': {'inner3': 3}}
    
    print(f"Size of nested list: {sys.getsizeof(nested_list)} bytes")
    print(f"Size of nested dictionary: {sys.getsizeof(nested_dict)} bytes")
    
  2. Output.
    Size of nested list: 80 bytes
    
    Size of nested dictionary: 184 bytes

2. Using Pympler for Advanced Memory Profiling.

  1. Besides the `sys` module, Pympler is another powerful tool for advanced memory profiling in Python.
  2. It provides a deeper insight into the memory consumption of Python objects, especially for complex data structures.

2.1 Installing Pympler.

  1. To install the Pympler library, use pip: pip install pympler.
    $ pip install pympler
    Collecting pympler
      Downloading Pympler-1.0.1-py3-none-any.whl (164 kB)
         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 164.8/164.8 kB 50.3 kB/s eta 0:00:00
    Installing collected packages: pympler
    Successfully installed pympler-1.0.1
    
  2. Run the command pip show pympler to verify the pympler library is installed successfully.
    $ pip show pympler
    Name: Pympler
    Version: 1.0.1
    Summary: A development tool to measure, monitor and analyze the memory behavior of Python objects.
    Home-page: https://github.com/pympler/pympler
    Author: Jean Brouwers, Ludwig Haehne, Robert Schuppenies
    Author-email: 
    License: Apache License, Version 2.0
    Location: /Users/songzhao/anaconda3/lib/python3.11/site-packages
    Requires: 
    Required-by: 
    

2.2 Example: Utilizing Pympler to Measure Object Size.

  1. Example source code.
    from pympler import asizeof
    
    # Measuring the size of a simple list
    simple_list = [1, 2, 3, 4, 5]
    print(f"Size of list using Pympler: {asizeof.asizeof(simple_list)} bytes")
  2. Output.
    Size of list using Pympler: 264 bytes

3. Comparing sys.getsizeof() and Pympler.

  1. While `sys.getsizeof()` provides a basic understanding of the memory consumption of Python objects, Pympler offers a more comprehensive and accurate assessment, especially for intricate data structures.
  2. The key difference lies in how they handle complex objects and their referenced components.
  3. Pympler calculates the complete memory footprint, including referenced objects, providing a more realistic representation of memory usage.

4. Conclusion.

  1. Understanding the memory usage of Python objects proves to be invaluable, especially in scenarios where memory optimization is paramount.
  2. The `sys.getsizeof()` function serves as an initial tool to estimate the size of basic data types and simple data structures.
  3. For more advanced memory profiling, Pympler provides a powerful solution to analyze complex data structures such as class instances with attributes more accurately.
  4. By mastering the art of measuring Python object sizes, developers can create more memory-efficient applications, ensuring optimal performance and scalability.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.