【发布时间】:2013-06-03 18:26:41
【问题描述】:
最近,我对 python 的内存管理感到困惑。首先是关于 dict,说我有 一个复合 dict 对象,如
d = {id1: {'x': 'a', 'y': [1,2,3], 'z': {'k', 'v'}}, id2: {...}}
如果我调用 del,
del d[id1]
d[id1]['y'] 和 d[id1]['z'] 会一起回收吗?
第二个是关于列表的,我看了here的答案,所以我试了一下。这是我的代码
import sys
import gc
import time
from collections import defaultdict
from pprint import pprint
def f():
d = defaultdict(int)
objects = gc.get_objects()
for o in objects:
d[type(o)] += 1
x = d.items()
x = sorted(x, key=lambda i: i[1], reverse=True)
pprint(x[:5])
def loop():
while True:
leaked = [[x] for x in range(100)]
f()
time.sleep(0.1)
当范围是 100 时,函数 f 确实向我显示列表在增加,但是当 我将范围修改为1000,没有什么可改变的,列表的数量保持不变。 谁能告诉我这是什么问题?
【问题讨论】: