深版 sys.getsizeof [英] Deep version of sys.getsizeof
问题描述
我想计算一个对象使用的内存.sys.getsizeof
很棒,但是很浅(例如,在列表上调用,它不包括列表元素占用的内存).
我想编写一个通用的深度"版本的 sys.getsizeof
.我理解深"的定义有些含糊;我对 定义后跟 copy.deepcopy
<非常满意/a>.
这是我的第一次尝试:
def get_deep_sizeof(x, level=0, processes=None):如果处理为无:# 只有当这个函数被客户端代码调用时才会出现,而不是递归调用处理 = 设置()处理.添加(id(x))mem = sys.getsizeof(x)如果 isinstance(x, collections.Iterable) 而不是 isinstance(x, str):对于 x 中的 xx:如果已处理 id(xx):继续mem += get_deep_sizeof(xx,level+1,已处理)如果 isinstance(x, dict):mem += get_deep_sizeof(x[xx], 级别+1, 已处理)返回内存
它存在两个已知问题,以及未知数量的未知问题:
- 我不知道如何以捕获所有链接对象的方式遍历通用容器.因此,我使用
in
进行迭代,并对字典的大小写进行硬编码(包括值,而不仅仅是键).显然,这不适用于字典等其他类. - 我不得不硬编码排除
str
(这是一个可迭代的,但没有指向任何其他对象的链接).同样,如果有更多类似的对象,这将中断.
我怀疑使用 in
不是一个好主意,但我不知道还能做什么.
我认为 Pympler已经在这一点上击败了你.
来自他们的文档:
<预><代码>>>>从 pympler.asizeof 导入 asizeof>>>obj = [1, 2, (3, 4), '文本']>>>大小(对象)176可以在此处找到源代码.
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).
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
.
Here's my first attempt:
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:
- 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.
I suspect that using in
is not a good idea, but I'm not sure what else to do.
I think that Pympler has already beaten you to the punch on this one.
From their documentation:
>>> from pympler.asizeof import asizeof
>>> obj = [1, 2, (3, 4), 'text']
>>> asizeof(obj)
176
The source code can be found here.
这篇关于深版 sys.getsizeof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!