Python 结构体的内存大小
32位和64位平台上Python数据结构的内存大小有参考吗?
如果没有,那么将其放在 SO 上就好了。越详尽越好!那么以下 Python 结构使用了多少字节(取决于 len 和相关的内容类型)?
int
float
- 引用
str
- unicode 字符串
tuple
list
dict
>set
array.array
numpy.array
deque
- 新式类对象
- 旧式类对象
- ...以及我忘记的一切!
(对于只保留对其他对象的引用的容器,我们显然不想计算项目本身的大小,因为它可能是共享的。)
此外,有没有一种方法可以获取对象在运行时使用的内存(递归地 )或不)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
之前的问题的建议对此是使用 sys.getsizeof(),引用:
你可以采用这种方法:
2012-09-30
python 2.7(linux,32位):
python 3.3(linux,32位)
2016-08-01
OSX,Python 2.7.10(默认,2015年10月23日,19:19: 21) 达尔文上的 [GCC 4.2.1 兼容 Apple LLVM 7.0.0 (clang-700.0.59.5)]
The recommendation from an earlier question on this was to use sys.getsizeof(), quoting:
You could take this approach:
2012-09-30
python 2.7 (linux, 32-bit):
python 3.3 (linux, 32-bit)
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
这些答案都收集浅层尺寸信息。我怀疑这个问题的访问者最终会在这里寻求回答这个问题:“内存中这个复杂的对象有多大?”
这里有一个很好的答案:https://goshippo.com/blog/ measure-real-size-any-python-object/
妙语:
像这样使用:
如果你想更深入地了解Python的内存模型,这里有一篇很棒的文章,其中有类似的“总大小”代码片段作为较长解释的一部分: https://code.tutsplus.com/tutorials/understand-how-much-memory-your-python-objects-use--cms-25609
These answers all collect shallow size information. I suspect that visitors to this question will end up here looking to answer the question, "How big is this complex object in memory?"
There's a great answer here: https://goshippo.com/blog/measure-real-size-any-python-object/
The punchline:
Used like so:
If you want to know Python's memory model more deeply, there's a great article here that has a similar "total size" snippet of code as part of a longer explanation: https://code.tutsplus.com/tutorials/understand-how-much-memory-your-python-objects-use--cms-25609
我一直很高兴使用 pympler 来完成此类任务。它与许多版本的 Python 兼容——
asizeof
模块尤其可以追溯到 2.2!例如,使用 Hughdbrown 的示例,但在开头使用
from pympler import asizeof
,在结尾使用print asizeof.asizeof(v)
,我看到(MacOSX 10.5 上的系统 Python 2.5 ):显然这里有一些近似值,但我发现它对于足迹分析和调整非常有用。
I've been happily using pympler for such tasks. It's compatible with many versions of Python -- the
asizeof
module in particular goes back to 2.2!For example, using hughdbrown's example but with
from pympler import asizeof
at the start andprint asizeof.asizeof(v)
at the end, I see (system Python 2.5 on MacOSX 10.5):Clearly there is some approximation here, but I've found it very useful for footprint analysis and tuning.
尝试内存分析器。
内存分析器
Try memory profiler.
memory profiler
您也可以使用 guppy 模块。
和:
Also you can use guppy module.
And:
当您使用
dir([object])
内置函数时,您可以获取内置函数的__sizeof__
。When you use the
dir([object])
built-in function, you can get the__sizeof__
of the built-in function.还可以使用
tracemalloc
模块来自Python标准库。对于那些用 C 实现的类的对象来说,它似乎工作得很好(例如,与 Pympler 不同)。One can also make use of the
tracemalloc
module from the Python standard library. It seems to work well for objects whose class is implemented in C (unlike Pympler, for instance).