sys.getsizeof的深版本 [英] Deep version of 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屋!