[英]Python Dict Switcher results in memory leak
I have extensively read about immutable and mutable objects in Python for a couple of months now and I seem to begin to understand the concept. 几个月以来,我已经广泛阅读了有关Python中不可变和可变对象的信息,而且我似乎已经开始理解这个概念。 Still I cannot spot the problem why my code below produces memory leaks. 我仍然无法发现下面我的代码为什么会产生内存泄漏的问题。 The dicts function as references to immutable records of specific type. 字典用作对特定类型的不可变记录的引用。 In many cases, I get an update of an existing record, in this case, the existing record will only be updated if the two records ( oldrecord
and newrecord
) are not equal. 在许多情况下,我会更新现有记录,在这种情况下,仅当两个记录( oldrecord
和newrecord
)不相等时,才会更新现有记录。 However, I have the feeling that newrecord
gets never deleted if oldrecord
and newrecord
match, although all references appear to cease to exist in such a case. 但是,我觉得如果oldrecord
和newrecord
匹配, newrecord
不会被删除,尽管在这种情况下所有引用似乎都不存在了。
My question: Is the code below good practice for selecting a reference to a dict based on record type or should I do it differently (eg through dictSwitcher
)? 我的问题:下面的代码是基于记录类型选择对dict的引用的良好实践还是我应该做不同的事情(例如,通过dictSwitcher
)?
class myRecordDicts():
def __init__(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 dictSelector(self, record):
dictSwitcher = {
myCustomRecordType1().name: self.type1Dict,
myCustomRecordType2().name: self.type2Dict,
myCustomRecordType3().name: self.type3Dict,
myCustomRecordType4().name: self.type4Dict,
myCustomRecordType5().name: self.type5Dict,
myCustomRecordType6().name: self.type6Dict,
myCustomRecordType7().name: self.type7Dict,
}
return dictSwitcher.get(record.name)
def AddRecordToDict(self, newrecord):
dict = self.dictSelector(newrecord)
recordID = newrecord.id
if recordID in dict:
oldrecord = dict[recordID]
self.MergeExistingRecords(oldrecord,newrecord)
else:
dict[recordID] = newrecord
def MergeExistingRecords(self, oldrecord, newrecord):
# Basic Compare function
oldRecordString = oldrecord.SerializeToString()
newRecordString = newrecord.SerializeToString()
# no need to do anything if same length
if not len(oldRecordString) == len(newRecordString):
oldrecord.CustomMergeFrom(newrecord)
Well, it seems always like that: I was working for hours on this problem and could not make progress. 好吧,似乎总是这样:我在这个问题上工作了几个小时,无法取得进展。 5 Minutes after formulating the question correctly on StackExchange, I found my issue: 在StackExchange上正确制定问题后5分钟,我发现了我的问题:
I needed to remove the references in init since I was never passing dicts when instantiating myRecordsDicts()
, the following code does not leak memory: 我需要删除init中的引用,因为在实例化myRecordsDicts()
时从未传递过命令,因此以下代码不会泄漏内存:
class myRecordDicts():
def __init__(self):
self.type1Dict = dict()
self.type2Dict = dict()
self.type3Dict = dict()
self.type4Dict = dict()
self.type5Dict = dict()
self.type6Dict = dict()
self.type7Dict = dict()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.