python sizeof函数_在Python中准确测量对象大小-Sys.GetSizeOf不起作用

I am trying to accurately/definitively find the size differences between two different classes in Python. They are both new style classes, save for one not having slots defined. I have tried numerous tests to determine their size difference, but they always end up being identical in memory usage.

So far I have tried sys.GetSizeOf(obj) and heapy's heap() function, with no positive results. Test code is below:

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()

Output:

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.>

This is all with Python 2.6.0. I also attempted to override the class's sizeof methods to try determine the size by summing the individual sizeofs but that didn't yield any different results:

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__()

Results with the sizeof method overridden:

Sizeof test3_obj 80

Sizeof test4_obj 80

解决方案

sys.getsizeof returns a number which is more specialized and less useful than people think. In fact, if you increase the number of attributes to six, your test3_obj remains at 32, but test4_obj jumps to 48 bytes. This is because getsizeof is returning the size of the PyObject structure implementing the type, which for test3_obj doesn't include the dict holding the attributes, but for test4_obj, the attributes aren't stored in a dict, they are stored in slots, so they are accounted for in the size.

But a class defined with __slots__ takes less memory than a class without, precisely because there is no dict to hold the attributes.

Why override __sizeof__? What are you really trying to accomplish?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值