【发布时间】:2016-10-01 18:45:05
【问题描述】:
看起来垃圾收集器没有从 python 2.7 中的dict() 收集值pop'd(没有在 python 3 上尝试)。示例如下:
a = dict()
# fill the memory (dict)
for i in xrange(0, 9999999):
a[i] = i
# Memory usage is about 600 MB
# try to free the memory
for i in xrange(0, 9999999):
a.pop(i)
# print the dict and see it is empty
print "%r" % a
# prints: {}
# Memory usage is about 600 MB
import copy
a = copy.copy(a)
# Memory usage decreased to about 200 MB
import gc
gc.collect()
# Memory usage decreased to about 10 MB
有人知道为什么会发生这种情况以及如何解决这个内存泄漏问题吗?
【问题讨论】:
-
你是如何测量内存使用的?
-
使用
top命令 -
为什么使用
pop而不是del? -
@ivanK。
del的行为也相同。 -
top没有提供准确的数字,因为内存块不会立即释放。
标签: python dictionary memory memory-management memory-leaks