Created
June 9, 2022 00:57
-
-
Save brunokim/974953c166eaabd18ebb81e1940e6304 to your computer and use it in GitHub Desktop.
This file contains 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 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