Explain Codes LogoExplain Codes Logo

How do I determine the size of an object in Python?

python
memory-management
object-sizing
python-objects
Anton ShumikhinbyAnton Shumikhin·Oct 9, 2024
TLDR

Get an object’s memory footprint instantly using sys.getsizeof() in Python:

import sys bytes_taken = sys.getsizeof(your_object)

Swap your_object with any variable to know its memory footprint in bytes. Remember, this just returns the memory held directly by the object, not counting anything it refers to if it's a composite object.

A closer look: Precision and exceptions with getsizeof()

sys.getsizeof() is your no-frills workhorse for sizing up objects. However, it might underestimate the actual memory usage. It doesn't consider reference objects' memory and for custom or third-party objects, the method may not give precise output because of their unique memory footprint or internals. In these scenarios, for accurate measurements, consider implementing a __sizeof__ method in your classes or use the more encompassing and precise Pympler's asizeof module.

Understanding memory structures: Containers, Custom Objects and numpy arrays

There are certain gotchas with Python memory sizing. For containers like Lists, sets, dicts, memory can be preallocated making the reported size larger than the data present.

For custom containers and objects, sys.getsizeof() will not account for memory used by contained objects. One has to recursively calculate sizes using sys.getsizeof() or gc.get_referents() to acquire an accurate total size.

For numpy arrays, getsizeof can't compute the correct size as arrays have high internal fragmentation. However, numpy has the attribute nbytes to directly get the memory usage.

Handling tricky objects

When dealing with complex objects, perhaps custom classes or from third-party libraries, typical methods may not give the correct size, and this situation calls for pickle, dill or cloudpickle. One can serialize the object and estimate its size indirectly using something like len(pickle.dumps(your_object)) but don't forget, There's no such thing as a free lunch or in other words: Worst comes to worst, you might have pickles, but, no sandwich...