Last active
November 1, 2024 06:31
-
-
Save bosswissam/a369b7a31d9dcab46b4a034be7d263b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def get_size(obj, seen=None): | |
"""Recursively finds size of objects""" | |
size = sys.getsizeof(obj) | |
if seen is None: | |
seen = set() | |
obj_id = id(obj) | |
if obj_id in seen: | |
return 0 | |
# Important mark as seen *before* entering recursion to gracefully handle | |
# self-referential objects | |
seen.add(obj_id) | |
if isinstance(obj, dict): | |
size += sum([get_size(v, seen) for v in obj.values()]) | |
size += sum([get_size(k, seen) for k in obj.keys()]) | |
elif hasattr(obj, '__dict__'): | |
size += get_size(obj.__dict__, seen) | |
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)): | |
size += sum([get_size(i, seen) for i in obj]) | |
return size |
There is python module that provides similar functionality and other things as well such as tracking the memory consumption of the instances of a specific class, etc. called Pympler.
https://pympler.readthedocs.io/en/latest/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been happily using this code for a long time, but I just encountered a use case where this breaks down: a class built over a simple namedtuple data core. This pattern is desirable for certain multi-processing/cloud computing contexts.
These sizes should be almost the same, but they are not.
The problem is caused because
dat_obj
has an empty__dict__
and data stored in__iter__
.Here is the fix I made. It doesn't come out exactly the same, but it's a lot closer than before: