Skip to content

Instantly share code, notes, and snippets.

@brunokim
Created June 9, 2022 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunokim/974953c166eaabd18ebb81e1940e6304 to your computer and use it in GitHub Desktop.
Save brunokim/974953c166eaabd18ebb81e1940e6304 to your computer and use it in GitHub Desktop.
import gc
import sys
def get_transitive_size(obj):
stack = [obj]
size = 0
seen = set()
while stack:
o = stack.pop()
if isinstance(o, type):
continue
size += sys.getsizeof(o)
new_objs = set(gc.get_referents(o)) - seen
seen.update(new_objs)
stack.extend(new_objs)
return size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment