python: how to accurately obtain an object (such as dictionaries) memory footprint

       in python, sys module which has a getsizeof function, it can obtain an object's memory footprint, but be aware that this function can only get direct memory occupied by the object itself, the object pointed occupied memory references and It will not be counted; python, such as when the object is a container, then getsizeof function can not get inside the container contents of the memory footprint of all, just returned from the container itself memory footprint, such as a dictionary.

       In this regard, an official document gives a way, that is the definition of a function, all references to the container by recursive find and calculate its memory footprint, it can add up to return, and also gives the definition of the function of the source code, source code as follows (the site directly copy the official website given, due to the external network access excruciatingly slow, so the copy of this blog to call for immediate define).

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):
    """ Returns the approximate memory footprint an object and all of its contents.
    Automatically finds the contents of the following builtin containers and
    their subclasses:  tuple, list, deque, dict, set and frozenset.
    To search other containers, add handlers to iterate over their contents:
        handlers = {SomeContainerClass: iter,
                    OtherContainerClass: OtherContainerClass.get_elements}
    """
    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)


       But note that this function for ndarray still do not work, but fortunately ndarray can get its memory footprint size by direct nbytes property directly.

References:

The official document: https: //docs.python.org/3.7/library/sys.html#sys.getsizeof

Source Code: HTTPS: //code.activestate.com/recipes/577504/
----------------
Disclaimer: This article is CSDN blogger "S_o_l_o_n 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/S_o_l_o_n/article/details/102921863

Published 296 original articles · won praise 221 · views 540 000 +

Guess you like

Origin blog.csdn.net/qq_36387683/article/details/104991940