Python 结构的内存大小 [英] In-memory size of a Python structure

查看:32
本文介绍了Python 结构的内存大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有关于 Python 数据结构在 32 位和 64 位平台上的内存大小的参考?

如果没有,那么将它放在 SO 上会很好.越详尽越好!那么以下 Python 结构使用了多少字节(取决于 len 和相关的内容类型)?

  • int
  • 浮动
  • 参考
  • str
  • unicode 字符串
  • 元组
  • list
  • dict
  • 设置
  • array.array
  • numpy.array
  • deque
  • 新式类对象
  • 旧式类对象
  • ...以及我忘记的一切!

(对于只保留对其他对象的引用的容器,我们显然不想计算项目本身的大小,因为它可能是共享的.)

此外,有没有办法在运行时(递归或不递归)获取对象使用的内存?

解决方案

来自 之前的问题 是使用 sys.getsizeof(),引用:

<预><代码>>>>导入系统>>>x = 2>>>sys.getsizeof(x)14>>>sys.getsizeof(sys.getsizeof)32>>>sys.getsizeof('this')38>>>sys.getsizeof('这也是')48

您可以采用这种方法:

<预><代码>>>>导入系统>>>输入十进制>>>>>>d = {... "int": 0,...浮动":0.0,... "dict": dict(),...设置":设置(),...元组":元组(),...列表":列表(),... "str": "a",... "unicode": u"a",...十进制":十进制.十进制(0),...对象":对象(),... }>>>对于 k, v in sorted(d.iteritems()):... 打印 k, sys.getsizeof(v)...十进制 40字典 140浮动 1612清单 36对象 8第 116 集25号元组 28统一码 28

<小时>

2012-09-30

python 2.7(Linux,32 位):

十进制 36字典 136浮动 1612清单 32对象 8第 112 集22号线元组 24Unicode 32

python 3.3(Linux,32 位)

十进制 52第144话浮动 1614清单 32对象 8第 112 集26号元组 24统一码 26

<小时>

2016-08-01

OSX,Python 2.7.10(默认,2015 年 10 月 23 日,19:19:21)[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin

十进制 80字典 280浮动 2424清单 72对象 16第 232 集38号元组 56统一码 52

Is there a reference for the memory size of Python data stucture on 32- and 64-bit platforms?

If not, this would be nice to have it on SO. The more exhaustive the better! So how many bytes are used by the following Python structures (depending on the len and the content type when relevant)?

  • int
  • float
  • reference
  • str
  • unicode string
  • tuple
  • list
  • dict
  • set
  • array.array
  • numpy.array
  • deque
  • new-style classes object
  • old-style classes object
  • ... and everything I am forgetting!

(For containers that keep only references to other objects, we obviously do not want to count the size of the item themselves, since it might be shared.)

Furthermore, is there a way to get the memory used by an object at runtime (recursively or not)?

解决方案

The recommendation from an earlier question on this was to use sys.getsizeof(), quoting:

>>> import sys
>>> x = 2
>>> sys.getsizeof(x)
14
>>> sys.getsizeof(sys.getsizeof)
32
>>> sys.getsizeof('this')
38
>>> sys.getsizeof('this also')
48

You could take this approach:

>>> import sys
>>> import decimal
>>> 
>>> d = {
...     "int": 0,
...     "float": 0.0,
...     "dict": dict(),
...     "set": set(),
...     "tuple": tuple(),
...     "list": list(),
...     "str": "a",
...     "unicode": u"a",
...     "decimal": decimal.Decimal(0),
...     "object": object(),
... }
>>> for k, v in sorted(d.iteritems()):
...     print k, sys.getsizeof(v)
...
decimal 40
dict 140
float 16
int 12
list 36
object 8
set 116
str 25
tuple 28
unicode 28


2012-09-30

python 2.7 (linux, 32-bit):

decimal 36
dict 136
float 16
int 12
list 32
object 8
set 112
str 22
tuple 24
unicode 32

python 3.3 (linux, 32-bit)

decimal 52
dict 144
float 16
int 14
list 32
object 8
set 112
str 26
tuple 24
unicode 26


2016-08-01

OSX, Python 2.7.10 (default, Oct 23 2015, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin

decimal 80
dict 280
float 24
int 24
list 72
object 16
set 232
str 38
tuple 56
unicode 52

这篇关于Python 结构的内存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆