Last active
August 29, 2015 14:19
-
-
Save 83tb/5ec561d8c2496012f2c5 to your computer and use it in GitHub Desktop.
Get total size of Python complex objects
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
from __future__ import print_function | |
from sys import getsizeof, stderr | |
from itertools import chain | |
from collections import deque | |
try: | |
from reprlib import repr | |
except ImportError: | |
pass | |
def total_size(o, handlers={}, verbose=False): | |
dict_handler = lambda d: chain.from_iterable(d.items()) | |
all_handlers = {tuple: iter, | |
list: iter, | |
deque: iter, | |
dict: dict_handler, | |
set: iter, | |
frozenset: iter, | |
} | |
all_handlers.update(handlers) # user handlers take precedence | |
seen = set() # track which object id's have already been seen | |
default_size = getsizeof(0) # estimate sizeof object without __sizeof__ | |
def sizeof(o): | |
if id(o) in seen: # do not double count the same object | |
return 0 | |
seen.add(id(o)) | |
s = getsizeof(o, default_size) | |
if verbose: | |
print(s, type(o), repr(o), file=stderr) | |
for typ, handler in all_handlers.items(): | |
if isinstance(o, typ): | |
s += sum(map(sizeof, handler(o))) | |
break | |
return s | |
return sizeof(o) | |
d = dict(a=1, b=2, c=3, d=[4,5,6,7], e='a string of chars') | |
print(total_size(d, verbose=True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment