Skip to content

Instantly share code, notes, and snippets.

@durden
Last active February 13, 2023 13:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durden/0b93cfe4027761e17e69c48f9d5c4118 to your computer and use it in GitHub Desktop.
Save durden/0b93cfe4027761e17e69c48f9d5c4118 to your computer and use it in GitHub Desktop.
Get size of Python object recursively to handle size of containers within containers
##### Taken from https://github.com/bosswissam/pysize
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, (file, str, bytes, bytearray)):
size += sum([get_size(i, seen) for i in obj])
return size
@Neeratyoy
Copy link

Thanks for the code.
I believe the file in L25 is supported only for Python2? Removing that works on Python3+.

However, I eventually went with this package, which turned out to be pretty neat too!

@mvindiola1
Copy link

Thanks for sharing this snippet. I used it today here: https://discuss.ray.io/t/memory-issue-debugging/7573/5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment