Python Dict切换器导致内存泄漏

Python Dict切换器导致内存泄漏,python,dictionary,garbage-collection,immutability,Python,Dictionary,Garbage Collection,Immutability,几个月来,我广泛阅读了Python中的不可变和可变对象,我似乎开始理解这个概念。我仍然无法发现下面的代码产生内存泄漏的原因。dicts的功能是引用特定类型的不可变记录。在许多情况下,我会更新现有记录,在这种情况下,只有当两个记录(oldrecord和newrecord)不相等时,才会更新现有记录。然而,我感觉如果oldrecord和newrecord匹配,newrecord永远不会被删除,尽管在这种情况下所有引用似乎都不存在 我的问题: 下面的代码是基于记录类型选择dict引用的良好实践,还是我

几个月来,我广泛阅读了Python中的不可变和可变对象,我似乎开始理解这个概念。我仍然无法发现下面的代码产生内存泄漏的原因。dicts的功能是引用特定类型的不可变记录。在许多情况下,我会更新现有记录,在这种情况下,只有当两个记录(
oldrecord
newrecord
)不相等时,才会更新现有记录。然而,我感觉如果
oldrecord
newrecord
匹配,
newrecord
永远不会被删除,尽管在这种情况下所有引用似乎都不存在

我的问题: 下面的代码是基于记录类型选择dict引用的良好实践,还是我应该采取不同的做法(例如通过
dictSwitcher

类myRecordDicts():
定义初始化(self,type1Dict=dict(),type2Dict=dict(),
type3Dict=dict()、type4Dict=dict()、type5Dict=dict()、type6Dict=dict()、type7Dict=dict()):
self.type1Dict=type1Dict
self.type2Dict=type2Dict
self.type3Dict=type3Dict
self.type4Dict=type4Dict
self.type5Dict=type5Dict
self.type6Dict=type6Dict
self.type7Dict=type7Dict
def选择器(自我、记录):
录音切换器={
myCustomRecordType1()。名称:self.type1Dict,
myCustomRecordType2()。名称:self.type2Dict,
myCustomRecordType3()。名称:self.type3Dict,
myCustomRecordType4()。名称:self.type4Dict,
myCustomRecordType5()。名称:self.type5Dict,
myCustomRecordType6()。名称:self.type6Dict,
myCustomRecordType7()。名称:self.type7Dict,
}
返回dictSwitcher.get(record.name)
def AddRecordToDict(自我,新记录):
dict=self.dictSelector(newrecord)
recordID=newrecord.id
如果在dict中记录ID:
oldrecord=dict[recordID]
self.MergeExistingRecords(旧记录、新记录)
其他:
dict[recordID]=新记录
def合并现有记录(自我、旧记录、新记录):
#基本比较函数
oldRecordString=oldrecord.SerializeToString()
newRecordString=newrecord.SerializeToString()
#如果长度相同,则无需执行任何操作
如果不是len(oldRecordString)==len(newRecordString):
oldrecord.CustomMergeFrom(新记录)

嗯,似乎总是这样:我在这个问题上工作了好几个小时,没有取得进展。在StackExchange上正确表述问题5分钟后,我发现了我的问题:

我需要删除init中的引用,因为我在实例化
myRecordsDicts()
时从未传递dicts,以下代码不会泄漏内存:

类myRecordDicts():
定义初始化(自):
self.type1Dict=dict()
self.type2Dict=dict()
self.type3Dict=dict()
self.type4Dict=dict()
self.type5Dict=dict()
self.type6Dict=dict()
self.type7Dict=dict()