To get the size of a Python object, you can use the
getsizeof() function from the
sys module. This function returns the size of an object in bytes.
For example, let’s print the size of a list and the size of an integer:
import sys l = [1, 2, 3, 4, 5] # Get the size of the object in bytes object_size = sys.getsizeof(l) print(object_size) print(12)
⚠️ Keep in mind that
sys.getsizeof() function only returns the size of the object itself. It does NOT take into account the size of inner objects. For instance, measuring the size of a list of lists with
getsizeof() method will only return the size of the outer list and not take into account the sizes of the inner lists.
The Big Issue with sys.getsizeof() Function
To illustrate the problem that arises when measuring object sizes with
- Let’s measure the size of a list of numbers.
- Let’s compare the size to a list that consists of 5 lists of numbers.
Here’s the code:
import sys nums = [1, 2, 3, 4, 5] print(sys.getsizeof(nums)) more_nums = [ [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5] ] print(sys.getsizeof(more_nums))
By looking at the above, you would expect
more_nums to be bigger in size than
nums. This is because
more_nums is a list that has 5 similar lists to the
But here’s what the output says:
What?! The sizes of the lists are the same even though they’re clearly different sizes.
As a matter of fact, the list length will always be the same as long as the number of objects is the same.
For example, let’s measure the size of a list of 5 characters:
import sys nums = ['t', 'e', 's', 't', 's'] print(sys.getsizeof(nums))
sys.getsizeof() function only works when measuring the size of a built-in object. But if you’re interested in the true size of the object among its contents objects, you need to pick a different approach.
How to Measure the True Size of a Python Object?
Because of the limitations in
sys.getsizeof() function, you won’t be able to determine the size of a nested object in Python. For example, you won’t be able to measure the size of a list of lists or a list of strings reliably.
A better way to calculate the size of a Python object is to use a recursive function that iterates the objects of the objects to check their size to accumulate the total size of the object.
To keep it in scope, we’re not going to write such a recursive function.
Instead, you can use an existing solution called Pympler, which is a free Python package you can install with pip.
$ pip install pympler
pympler installed, you can start estimating the size of your Python objects by using the
from pympler import asizeof nums = [1, 2, 3, 4, 5] print(asizeof.asizeof(nums)) more_nums = [ [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5] ] print(asizeof.asizeof(more_nums))
Looks better! The
asizeof() function not only calculates the space an object takes but also combines it with what the nested elements eat up. Now the list of lists is bigger than a single list of numbers which makes sense.
And if you expected the list of lists to take 5 x more space than the list, you were wrong.
Intuitively, that would be the case. But behind the scenes, a “lot of” memory is spent building the list, the list inside a list before adding a single integer. This initial cost of building the objects might change the size expectations of the objects you’re measuring.
For example, to build an empty list with an empty list inside, you already need 120 bytes of memory.
from pympler import asizeof print(asizeof.asizeof([])) # Output: 120
Then the rest of the memory consumption is based on what values you add to the inner lists and how many inner lists you’ll have.
Today you learned how to measure the size of a Python object.
To take home, the out-of-box method
sys.getsizeof() is limited and doesn’t care what’s inside the objects. In other words, most of the time it doesn’t return the size you want.
A better and more intuitive way to measure the true size of an object is by measuring the inner object’s size too. You can do this with the Pympler package
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
- Artificial Intelligence2023.01.10How to Use AI Art Generators—Input Design (Prompt Engineering)
- Favorites2022.11.30ChatGPT—What Is It? How to Use It? (Examples, Tips & More)