简体   繁体   English

Python - 嵌套空列表的内存大小

[英]Python - In-memory size of nested empty lists

The size in memory of an object can be gotten with sys.getsizeof . 可以使用sys.getsizeof对象的内存大小。

As one could expect, the size of [] is smaller than the size of [[]] . 可以预料, []的大小小于[[]]的大小。 On my machine, I get the following sizes: 在我的机器上,我得到以下尺寸:

>>> sys.getsizeof([])
36
>>> sys.getsizeof([[]])
40

Now, whatever the number of nested empty lists I have, I always get the same size: 现在,无论我拥有多少嵌套空列表,我总是得到相同的大小:

>>> sys.getsizeof([[[]]])
40
>>> sys.getsizeof([[[[]]]])
40

What is the reason why the size of nested empty lists seems to have an upper boundary? 嵌套空列表的大小似乎有上边界的原因是什么?

The size just refers to the outermost object and not the nested ones. 大小只是指最外面的对象而不是嵌套的对象。 From the perspective of getsizeof the object size is just the size of the object plus the size of the pointers contained in the object not the objects being pointed to. getsizeof的角度来看,对象大小只是对象的大小加上对象中包含的指针的大小而不是指向的对象。 You can see this from the following: 您可以从以下内容中看到:

>>> import sys
>>> sys.getsizeof([])
64
>>> sys.getsizeof([[]])
72
>>> sys.getsizeof([[[]]])
72
>>> sys.getsizeof([[],[]])
80
>>> sys.getsizeof([[[]],[[]]])
80

If you want to get the total memory footprint you will either need to recursively find the sizes for the object or use some other memory profiling. 如果要获得总内存占用量,则需要以递归方式查找对象的大小或使用其他内存分析。

Also if you are writing your own objects and want getsizeof to correctly return the size you can implement your own __sizeof__ method. 此外,如果您正在编写自己的对象并希望getsizeof正确返回大小,则可以实现自己的__sizeof__方法。 For example: 例如:

import sys
class mylist:
    def __init__(self, iterable):
        self.data = list(iterable)

    def __sizeof__(self):
        return object.__sizeof__(self) + \
            sum(sys.getsizeof(v) for v in self.__dict__.values()) + \
            sum(sys.getsizeof(item) for item in self.data)

original_data = [[1,2,3], [1,2,3]]
print(sys.getsizeof(original_data))
foo = mylist(original_data)
print(sys.getsizeof(foo))

Results: 结果:

~/code_snippets$ python3 sizeof_list.py 
80
336

Reading the documentation would have taught me that when calling getsizeof , 阅读文档会告诉我,当调用getsizeof

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to. 仅考虑直接归因于对象的内存消耗,而不考虑它所引用的对象的内存消耗。

Since [] is a container, its size, according to getsizeof , is its own size plus the size of the references it contains, but not the sizes of the objects referred to. 由于[]是一个容器,因此根据getsizeof ,它的大小是它自己的大小加上它包含的引用的大小,而不是引用的对象的大小。

Therefore, if [] has a size of 36 , and a reference has a size of 4 , then the size of [[]] is 36+4 , hence 40 . 因此,如果[]的大小为36 ,参考的大小为4 ,则[[]]的大小为36+4 ,因此为40

Now, [[[]]] is nothing more than [x] where x is a reference to [[]] . 现在, [[[]]]只不过是[x] ,其中x是对[[]]的引用。 Hence, the size of [[[]]] is the size of [] plus the size of a reference, so 40 as well. 因此, [[[]]]的大小是[]的大小加上参考的大小,所以也是40

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM