python - Python中字典的内存使用情况?

标签 python dictionary memory-management

当我在字典的 sys 模块中使用 getsizeof 方法时,我有点困惑。下面我创建了一个包含两个字符串的简单字典。这两个字符串的大小明显大于字典中的一个。字典大小可能只是字典的开销,即它不考虑实际数据。找出整个字典的内存使用情况(键、值、字典开销)的最佳方法是什么?

>>> first = 'abc'*1000
>>> second = 'def'*1000
>>> my_dictionary = {'first': first, 'second': second}
>>> getsizeof(first)
3021
>>> getsizeof(second)
3021
>>> getsizeof(my_dictionary)
140

最佳答案

来自PythonDocs

See recursive sizeof recipe for an example of using getsizeof() recursively to find the size of containers and all their contents.

所以它只计算开销,但你可以使用this link中的函数为像字典这样的容器计算它。

关于python - Python中字典的内存使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6579757/

相关文章:

python - Pygame.draw.rect 语法无效

python - 什么可以关闭 websocket 连接?

python - 逻辑与按位运算符 AND

Python/Selenium 使用 Firefox 下载 PDF

java - 如何使用 HashMap 写入和读取文件?

swift - 如何使用 Swift 3 访问字典值?

c# - <Type, List<Type>> 的字典

macos - 帮助改进我针对 Safari 高内存使用率的个人解决方案,或说明为什么它不好

ios - 在 UIScrollView 的 Paging 上重用 3 个 View

c - ubuntu gcc 中的段错误