这是我的代码:
from memory_profiler import profile
@profile
def mess_with_memory():
huge_list = range(20000000)
del huge_list
print "why this kolaveri di?"
当我从解释器运行它时,这就是输出:
Line # Mem 使用增量行内容
3 7.0 MiB 0.0 MiB @profile
4 def mess_with_memory():
5
6 628.5 MiB 621.5 MiB huge_list = range(20000000)
7 476.0 MiB -152.6 MiB del huge_list
8 476.0 MiB 0.0 MiB print "why this kolaveri di"
如果您注意到输出,创建巨大的列表消耗了 621.5 MB,而删除它只释放了 152.6 MB。当我检查文档时,我发现了以下语句:
the statement del x removes the binding of x from the namespace referenced by the local scope
所以我猜,它并没有删除对象本身,而是取消绑定它。但是,它释放了这么多空间(152.6 MB)在解除绑定时做了什么。有人可以痛苦地向我解释这里发生了什么吗?