How do find the size of an object in Python with an example

There are various methods available for finding the size of an object in Python.

An object can be a variable of built-in types, an array, or a dictionary of elements.

In many instances, when working with sizable data or objects in Python code, it becomes crucial to analyze the object size for inspection or debugging to:

  • Identify and address memory issues, ultimately reducing memory consumption.
  • Profile an application to optimize code and enhance the efficiency of loading objects quickly.
  • Gain insights into effective resource utilization, especially in lightweight applications on small-scale devices.

This tutorial aims to help developers understand how to find the size of an object in Python, facilitating the aforementioned objectives.

Use Python sys getsizeof() method

sys is a standalone module, that provides the getsizeof() method to return the memory size of an object

It returns memory size in bytes.

Syntax:

import sys
size = sys.getsizeof(object)

Takes a Python object, and returns the memory size in bytes.

import sys

arrayStrs = ["one", "two", "three", "four", "five","six"]
size = sys.getsizeof(arrayStrs)

print(f"Total array size is {size} bytes")

Notes:

  • sys is a standalone module with no dependencies required for inclusion and easy size retrieval.
  • It is best suited for simple objects such as arrays and isolated objects.
  • However, it is not recommended for use with complex objects that have references to other objects, commonly known as “nested objects.” sys only provides the size of simple objects and does not offer complete memory usage information.
  • When dealing with nested objects or arrays containing references to objects, it is advisable to use third-party libraries.

Use pympler asize method

Pympler is a third-party library designed to offer analytics on object usage in Python. The asizeof function within Pympler is particularly useful, as it prints the size of an object in bytes. This method is effective for objects of all types, including those with object references to other objects. The size returned also includes the size of nested objects.

To integrate Pympler into your Python environment, follow the below

Install Pympler by executing the command: pip install pympler.

Syntax:

from pympler import asizeof

size = pympler .asizeof(object)

takes an object as an input parameter, returns size in bytes

from pympler import asizeof


size = asizeof.asizeof(obj)

arrayStrs = ["one", "two", "three", "four", "five","six"]
size = pympler .asizeof(arrayStrs)

print(f"Total array size is {size} bytes")

Notes:

  • Pympler is not part of the standard library; it requires a dependency on the third-party module, pympler.
  • Pympler provides comprehensive analytics on objects, including child references, making it valuable for profiling objects and assessing memory usage.
  • Particularly useful for debugging and profiling larger data objects to gain a complete understanding of their usage patterns.
  • It’s important to note that there is a performance overhead due to the complete traversal of the object graph to obtain the size simultaneously.
  • It is useful for complete insights compared wi

pickle.dumps method

pickle is a library used for the serialization and deserialization of an object. It converts an object to streams such as a disk or network for transmission., again converts transmission to an object.

dumps() method is used to convert an object to a stream of data format.

It supports all object types such as lists, arrays, dictionaries, and complex custom objects.

Syntax:

import pickle
len(pickle.dumps(object))

Here is an example

import pickle



arrayStrs = ["one", "two", "three", "four", "five","six"]
size = len(pickle.dumps(arrayStrs))

print(f"Total array size is {size} bytes")

Notes:

  • It involves an additional step of serialization and deserialization to determine the size of an object.
  • It is capable of working with objects and nested child references of all types.
  • Extra steps are necessary to convert the object to a stream of bytes, and this poses a disadvantage in terms of slight performance
  • Pickle is not included in the standard library; it requires a dependency on the third-party module, pickle.
  • While not recommended for determining the size of simple objects, it is valuable for inspecting and debugging larger objects, especially when profiling on lighter devices.

use objgraph bytestring_size method

Objgraph is a Python library designed for visually representing object hierarchies in graphs. It is utilized to inspect memory issues and identify circular references.

The objgraph bytestring_size() method iteratively visits objects in the graph, determining the size of each dependent object, and ultimately returns the final size.

Syntax:

import objgraph
size = objgraph.bytestring_size(object)

input is an object return size in bytes

import objgraph


size = asizeof.asizeof(obj)

arrayStrs = ["one", "two", "three", "four", "five","six"]
size = objgraph.bytestring_size(obj)

print(f"Total array size is {size} bytes") //97

Notes:

  • Objgraph is a third-party module that requires a dependency and is not part of the standard library.
  • Objgraph provides more accurate sizes for larger and more complex objects.
  • It is particularly valuable for debugging and profiling larger data objects, offering a comprehensive understanding of their memory usage.
  • It’s important to note that there is a performance overhead associated with the complete traversal of the object graph to obtain the size.
  • It is not useful and inefficient for determining the size of simple objects.

Conclusion

If your objects are small, you can utilize the standard library’s sys.getsizeof() without any additional dependencies.

For larger objects, especially those with nested structures, and if you require comprehensive insights into their usage, you have the option to use either the pympler.asizeof() or the objgraph.bytestring_size() method to analyze object usage.

You can make your choice based on your preferences and specific purposes.