sys.getsizeof的深版本 [英] Deep version of sys.getsizeof

查看:104
本文介绍了sys.getsizeof的深版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算对象使用的内存. sys.getsizeof很好,但是很浅(例如,在列表上调用,它不包括列表元素占用的内存).

I want to calculate the memory used by an object. sys.getsizeof is great, but is shallow (for example, called on a list, it would not include the memory taken by the list's elements).

我想编写sys.getsizeof的通用深度"版本.我了解深层"的定义有些含糊;我对定义后跟copy.deepcopy 感到非常满意.

I'd like to write a generic "deep" version of sys.getsizeof. I understand there is some ambiguity in the definition of "deep"; I'm perfectly happy with the definition followed by copy.deepcopy.

这是我的第一次尝试:

def get_deep_sizeof(x, level=0, processed=None):
    if processed is None:
        # we're here only if this function is called by client code, not recursively
        processed = set()
    processed.add(id(x))
    mem = sys.getsizeof(x)
    if isinstance(x, collections.Iterable) and not isinstance(x, str):
        for xx in x:
            if id(xx) in processed:
                continue
            mem += get_deep_sizeof(xx, level+1, processed)
            if isinstance(x, dict):
                mem += get_deep_sizeof(x[xx], level+1, processed)
    return mem

它遇到两个已知问题,并且存在未知数量的未知问题:

It suffers from two known problems, and an unknown number of unknown problems:

  • 我不知道如何以捕获所有链接对象的方式遍历通用容器.因此,我使用in进行了迭代,并对字典的大小写进行了硬编码(包括值,而不仅仅是键).显然,这不适用于字典等其他类.
  • 我不得不对str的排除进行硬编码(这是可迭代的,但是没有指向任何其他对象的链接).再次,如果有更多这样的对象,这将中断.
  • I don't know how to traverse a generic container in a way that captures all the linked objects. Therefore, I iterated using in, and hard coded the case of dictionary (to include values, and not just the keys). Obviously, this will not work for other classes like dictionary.
  • I had to hard code the exclusion of str (which is an iterable, and yet does not have links to any other objects). Again, this will break if there are more objects like that.

我怀疑使用in不是一个好主意,但我不确定还有其他事情.

I suspect that using in is not a good idea, but I'm not sure what else to do.

推荐答案

我认为 Pympler 具有已经打败你了.

I think that Pympler has already beaten you to the punch on this one.

从他们的文档中:

>>> from pympler.asizeof import asizeof
>>> obj = [1, 2, (3, 4), 'text']
>>> asizeof(obj)
176

如果您想要一个特定的示例,则可以找到asizeof 此处.

If you want a specific example, you can find the source for asizeof here.

这篇关于sys.getsizeof的深版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆