Check object memory in Python

We can use Pympler (https://pympler.readthedocs.io/en/latest/) to inspect an object’s memory in Python. It can get an object’s memory including their references.

Here is an example:

>>> from pympler import asizeof 
>>> obj = [1, 2, (3, 4), 'text'] 
>>> asizeof.asizeof(obj) 
176 
>>> print(asizeof.asized(obj, detail=1).format()) 
[1, 2, (3, 4), 'text'] size=176 flat=48 (3, 4) size=64 flat=32 'text' size=32 flat=32 1 size=16 flat=16 2 size=16 flat=16

Leave a comment

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