Python中字典的内存使用? [英] Memory-usage of dictionary in Python?

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

问题描述

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

I am slightly confused when I use the getsizeof method in the sys module for dictionaries. Below I have created a simple dictionary of two strings. The two strings' sizes are clearly larger than the one of the dictionary. The dictionary size is probably the dictionary overhead only, i.e., it doesn't take the actual data into account. What is the best way to figure out the memory-usage of the whole dictionary (keys, values, dictionary overhead)?

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


推荐答案

PythonDocs


有关使用getsizeof的示例,请参阅递归大小配方 ()递归地找到容器的大小及其所有内容。

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

所以它只计算开销,但你可以使用函数在此链接中为其计算容器,如dicts。

So it only counts the overhead, but you can use the function in this link to calculate it for containers like dicts.

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

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