Python数据结构的内存大小 [英] memory size of Python data structure

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

问题描述

如何找出Python数据结构的内存大小?我正在寻找类似的东西:

How do I find out the memory size of a Python data structure? I'm looking for something like:

sizeof({1:'hello', 2:'world'})

如果它递归计数每一个事情,这是非常好的。但即使是一个基本的非递归结果也是有帮助的。基本上我想要了解各种实现选项,如tuple v.s.列表v.s.在内存占用量方面。这很重要,因为我计划有数百万个对象被实例化。

It is great if it counts every thing recursively. But even a basic non-recursive result helps. Basically I want to get a sense of various implementation options like tuple v.s. list v.s. class in terms of memory footprint. It matters because I'm planning to have millions of object instantiated.

我目前的开发平台是CPython 2.6。

My current dev platform is CPython 2.6.

推荐答案

查看 sys.getsizeof 功能。根据文档,它返回一个对象的大小,以字节为单位,由对象的 __ sizeof __ 方法给出。

由于 Daniel 在评论中指出,它不是递归的;它只计算对象本身占用的字节,而不是其引用的其他对象。 递归计算的这个配方通过Python 3文档链接。

As Daniel pointed out in a comment, it's not recursive; it only counts bytes occupied by the object itself, not other objects it refers to. This recipe for a recursive computation is linked to by the Python 3 documentation.

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

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