当我创建两个相互引用的字典对象时,即使我将它们明确设置为空,它们也会保留在内存中。以下代码消耗 >1 GB 内存
Dim i
For i = 1 to 100000
leak
Next
Sub leak
Dim a, b
Set a = createObject("scripting.dictionary")
Set b = createObject("scripting.dictionary")
a.Add "dict1", b
b.Add "dict2", a
Set a = Nothing
Set b = Nothing
end sub
这与某些垃圾收集无关(VBScript 不这样做)。证明:当我改变a.Add "dict1", b
和a.Add "dict1", "foo"
改变b.Add "dict2", a
内存消耗时a.Add "dict2", "bar"
,内存消耗保持在合理的范围内。
顺便说一句,当字典引用自身时也会发生这种情况:
Sub leak
Dim a
Set a = createObject("scripting.dictionary")
a.Add "dict1", a
Set a = Nothing
end sub
是否有可能以在内存中也被销毁的方式销毁像这些交叉引用字典这样的对象?