使用wxPython进行python内存泄漏

时间:2012-04-03 07:29:52

标签: python debugging memory wxpython memory-leaks

我最近意识到,我的程序有一个内存泄漏,它很小,但要么,我认为它根本不应该存在。

导致内存泄漏的部分实际上会创建一个新的wx.Panel并使用wx.WindowSplitter将其添加到界面,然后删除面板和联合国 分裂窗口。

self.getAction(self, event):
    if self.lastEdit[0] != None:
        if self.lastEdit[4] == STATEMENT:
            dialog = EditStatement(self.API.editorSplitter,
                    self.API, self.lastEdit, self.statementUpdateClbk)
        elif self.lastEdit[4] == CONDITION:
            dialog = EditCondition(self.API.editorSplitter,
                    self.API, self.lastEdit, self.conditionUpdateClbk)
    # Show or hide split window
    self.splitEditor(dialog)

def splitEditor(self, editPanel):
    self.API.editorSplitter.Unsplit()
    if self.lastPanel != None:
        self.lastPanel.DissociateHandle()
        self.lastPanel.DestroyChildren()
        self.lastPanel.Destroy()
    self.lastPanel = editPanel
    if editPanel != None:
        self.API.editorSplitter.SplitVertically(self.API.tabManager,  
                                           editPanel, -305)

请注意,之前提及的EditStatement un EditConditionwx.Panel条款。在循环中调用getAction()会随机增加内存使用量,最奇怪的是,在某些迭代中没有变化,在某些迭代中使用的内存甚至有点下降,但在大多数迭代中使用的内存增长。以下是使用gc模块包装器进行调试的一些输出。

用于测试的代码:

def cycler(self):
    while True:
        self.getAction(None) #Show window
        wx.YieldIfNeeded()
        self.mem() # prints memory information
        self.splitEditor(None) #Hide window
        wx.YieldIfNeeded()
        self.mem() # prints memory information             

输出如下:

Window action -  Action      Object          Count

Create Window -  Added    StaticText  10
Create Window -  Changed  dict    +1
Create Window -  Added    GridBagSizer    1
Create Window -  Changed  list    +2
Create Window -  Added    CheckBox    4
Create Window -  Changed  ComboBox    +6
Create Window -  Added    EditStatement   1
Create Window -  Changed  BoxSizer    +1
Create Window -  Changed  instancemethod  +15

Create Window -  Count = 25453
Create Window -  Memory(rss): 42528

Destroy Window -  Changed dict    -1
Destroy Window -  Changed list    -2
Destroy Window -  Changed ComboBox    -6
Destroy Window -  Changed BoxSizer    -1
Destroy Window -  Changed instancemethod  -15
Destroy Window -  Deleted StaticText  10
Destroy Window -  Deleted GridBagSizer    1
Destroy Window -  Deleted CheckBox    4
Destroy Window -  Deleted EditStatement   1

Destroy Window -  Count = 25412
Destroy Window -  Memory(rss): 42528

Create Window -  Added    StaticText  10
Create Window -  Changed  dict    +1
Create Window -  Added    GridBagSizer    1
Create Window -  Changed  list    +2
Create Window -  Added    CheckBox    4
Create Window -  Changed  ComboBox    +6
Create Window -  Added    EditStatement   1
Create Window -  Changed  BoxSizer    +1
Create Window -  Changed  instancemethod  +15

Create Window -  Count = 25453
Create Window -  Memory(rss): 42536

Destroy Window -  Changed dict    -1
Destroy Window -  Changed list    -2
Destroy Window -  Changed ComboBox    -6
Destroy Window -  Changed BoxSizer    -1
Destroy Window -  Changed instancemethod  -15
Destroy Window -  Deleted StaticText  10
Destroy Window -  Deleted GridBagSizer    1
Destroy Window -  Deleted CheckBox    4
Destroy Window -  Deleted EditStatement   1

Destroy Window -  Count = 25412
Destroy Window -  Memory(rss): 42540

Create Window -  Added    StaticText  10
Create Window -  Changed  dict    +1
Create Window -  Added    GridBagSizer    1
Create Window -  Changed  list    +2
Create Window -  Added    CheckBox    4
Create Window -  Changed  ComboBox    +6
Create Window -  Added    EditStatement   1
Create Window -  Changed  BoxSizer    +1
Create Window -  Changed  instancemethod  +15

Create Window -  Count = 25453
Create Window -  Memory(rss): 42552

Destroy Window -  Changed dict    -1
Destroy Window -  Changed list    -2
Destroy Window -  Changed ComboBox    -6
Destroy Window -  Changed BoxSizer    -1
Destroy Window -  Changed instancemethod  -15
Destroy Window -  Deleted StaticText  10
Destroy Window -  Deleted GridBagSizer    1
Destroy Window -  Deleted CheckBox    4
Destroy Window -  Deleted EditStatement   1

Destroy Window -  Count = 25412
Destroy Window -  Memory(rss): 42556

Create Window -  Added    StaticText  10
Create Window -  Changed  dict    +1
Create Window -  Added    GridBagSizer    1
Create Window -  Changed  list    +2
Create Window -  Added    CheckBox    4
Create Window -  Changed  ComboBox    +6
Create Window -  Added    EditStatement   1
Create Window -  Changed  BoxSizer    +1
Create Window -  Changed  instancemethod  +15

Create Window -  Count = 25453
Create Window -  Memory(rss): 42576

正如您所看到的,使用的内存正在增加,但是在创建和删除后对象计数不会改变,我只是不明白可能会造成这样的内存泄漏。任何想法,将不胜感激。在45分钟内,内存使用量从~ 20 MB增加到~ 500 MB

P.S。如果有兴趣,可以在以下网址找到已创建面板的源代码:EditStatement EditCondition

P.S.S。我不确定,但我倾向于认为它是wx.Python bug,wx.SplitterWindow

0 个答案:

没有答案