当我在字典的 sys
模块中使用 getsizeof
方法时,我有点困惑。下面我创建了一个包含两个字符串的简单字典。这两个字符串的大小明显大于字典中的一个。字典大小可能只是字典的开销,即它不考虑实际数据。找出整个字典的内存使用情况(键、值、字典开销)的最佳方法是什么?
>>> first = 'abc'*1000
>>> second = 'def'*1000
>>> my_dictionary = {'first': first, 'second': second}
>>> getsizeof(first)
3021
>>> getsizeof(second)
3021
>>> getsizeof(my_dictionary)
140
最佳答案
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/