def total_size(obj, verbose=False):
    """ Returns approximate memory size"""
    seen = set()

    def sizeof(o):
        if id(o) in seen:
            return 0
        seen.add(id(o))
        s = sys.getsizeof(o, default=0)
        if verbose:
            print(s, type(o), repr(o))
        if isinstance(o, (tuple, list, set, frozenset, deque)):
            s += sum(map(sizeof, iter(o)))
        elif isinstance(o, dict):
            s += sum(map(sizeof, chain.from_iterable(o.items())))
        elif "__dict__" in dir(o):
            s += sum(map(sizeof, chain.from_iterable(o.__dict__.items())))
        return s

    return sizeof(obj)
def total_size ( obj , verbose = False ) : seen = set ( ) def sizeof ( o ) : if id ( o ) in seen : return 0 seen . add ( id ( o ) ) s = sys . getsizeof ( o , default = 0 ) if verbose : print ( s , type ( o ) , repr ( o ) ) if isinstance ( o , ( tuple , list , set , frozenset , deque ) ) : s += sum ( map ( sizeof , iter ( o ) ) ) elif isinstance ( o , dict ) : s += sum ( map ( sizeof , chain . from_iterable ( o . items ( ) ) ) ) elif "__dict__" in dir ( o ) : s += sum ( map ( sizeof , chain . from_iterable ( o . __dict__ . items ( ) ) ) ) return s return sizeof ( obj )
Returns approximate memory size