This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients amaury.forgeotdarc, loewis, pitrou, rhettinger, serhiy.storchaka
Date 2013-09-19.18:32:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
> How do you stop walking your graph if it spans the whole graph of Python objects?

We can stop at specific types of objects (for example types and modules).

> If sys.getsizeof() is only useful for people who know *already* how an
object is implemented internally, then it's actually useless, because
those people can just as well do the calculation themselves.

It's why sys.getsizeof() is a low-level tool. We need high-level tool in the stdlib. Even imperfect recursive counting will be better than confusing for novices sys.getsizeof().

> (By the way, OrderedDict.__sizeof__ already breaks the rule you are trying to impose)

Yes, I know, and I think it is wrong.

Here is improved version of gettotalsizeof():

def gettotalsizeof(*args, exclude_types=(type, type(sys))):
    seen = {}
    stack = []
    for obj in args:
        if id(obj) not in seen:
            seen[id(obj)] = obj
    sum = 0
    while stack:
        obj = stack.pop()
        sum += sys.getsizeof(obj)
        for obj in gc.get_referents(obj):
            if id(obj) not in seen and not isinstance(obj, exclude_types):
                seen[id(obj)] = obj
    return sum

>>> gettotalsizeof(sys)
>>> gettotalsizeof(gc)
>>> gettotalsizeof(sys.getsizeof)
>>> gettotalsizeof(gettotalsizeof)
>>> class C: pass
>>> gettotalsizeof(C)
>>> gettotalsizeof(C())
Date User Action Args
2013-09-19 18:32:32serhiy.storchakasetrecipients: + serhiy.storchaka, loewis, rhettinger, amaury.forgeotdarc, pitrou
2013-09-19 18:32:32serhiy.storchakasetmessageid: <>
2013-09-19 18:32:32serhiy.storchakalinkissue19048 messages
2013-09-19 18:32:32serhiy.storchakacreate