在Python中准确测量对象大小 - Sys.GetSizeOf无法正常工作

Zor*_*vic 8 python memory slots

我试图准确/明确地找到Python中两个不同类之间的大小差异.它们都是新的样式类,除了没有定义槽的一个.我已经尝试了很多测试来确定它们的大小差异,但它们总是在内存使用方面完全相同.

到目前为止,我已经尝试了sys.GetSizeOf(obj)和heapy的heap()函数,没有任何积极的结果.测试代码如下:

import sys
from guppy import hpy

class test3(object):
    def __init__(self):
        self.one = 1
        self.two = "two variable"

class test4(object):
    __slots__ = ('one', 'two')
    def __init__(self):
        self.one = 1
        self.two = "two variable"

test3_obj = test3()
print "Sizeof test3_obj", sys.getsizeof(test3_obj)

test4_obj = test4()
print "Sizeof test4_obj", sys.getsizeof(test4_obj)

arr_test3 = []
arr_test4 = []

for i in range(3000):
    arr_test3.append(test3())
    arr_test4.append(test4())

h = hpy()
print h.heap()
Run Code Online (Sandbox Code Playgroud)

输出:

Sizeof test3_obj 32
Sizeof test4_obj 32

Partition of a set of 34717 objects. Total size = 2589028 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  11896  34   765040  30    765040  30 str
     1   3001   9   420140  16   1185180  46 dict of __main__.test3
     2   5573  16   225240   9   1410420  54 tuple
     3    348   1   167376   6   1577796  61 dict (no owner)
     4   1567   5   106556   4   1684352  65 types.CodeType
     5     68   0   105136   4   1789488  69 dict of module
     6    183   1    97428   4   1886916  73 dict of type
     7   3001   9    96032   4   1982948  77 __main__.test3
     8   3001   9    96032   4   2078980  80 __main__.test4
     9    203   1    90360   3   2169340  84 type
<99 more rows. Type e.g. '_.more' to view.>
Run Code Online (Sandbox Code Playgroud)

这完全适用于Python 2.6.0.我还尝试覆盖类的sizeof方法,尝试通过对各个sizeofs求和来确定大小,但不会产生任何不同的结果:

class test4(object):
    __slots__ = ('one', 'two')
    def __init__(self):
        self.one = 1
        self.two = "two variable"
    def __sizeof__(self):
        return super(test4, self).__sizeof__() + self.one.__sizeof__() + self.two.__sizeof__()
Run Code Online (Sandbox Code Playgroud)

使用sizeof方法覆盖的结果:

Sizeof test3_obj 80
Sizeof test4_obj 80
Run Code Online (Sandbox Code Playgroud)

Eng*_*ero 6

正如其他人所说,sys.getsizeof仅返回代表数据的对象结构的大小。因此,例如,如果您有一个不断添加元素的动态数组,sys.getsizeof(my_array)则只会显示基础DynamicArray对象的大小,而不是其元素占用的不断增长的内存大小。

pympler.asizeof.asizeof()给出物体的大致完整尺寸,可能对您来说更准确。

from pympler import asizeof
asizeof.asizeof(my_object)  # should give you the full object size
Run Code Online (Sandbox Code Playgroud)


Ned*_*der 5

sys.getsizeof返回一个比人们想象的更专业但没那么有用的数字。事实上,如果将属性数量增加到 6 个,则 test3_obj 仍为 32 个字节,但 test4_obj 会跃升至 48 个字节。这是因为 getsizeof 返回实现该类型的 PyObject 结构的大小,对于 test3_obj 来说,它不包括保存属性的字典,但对于 test4_obj 来说,属性不存储在字典中,而是存储在插槽中,因此它们被计入尺寸。

但是用 定义的类比__slots__没有定义的类占用更少的内存,正是因为没有字典来保存属性。

为什么要覆盖__sizeof__?你真正想实现什么目标?