Memory leak in PlotWidget

418 views
Skip to first unread message

shx...@gmail.com

unread,
May 27, 2014, 9:19:41 AM5/27/14
to pyqt...@googlegroups.com
I encountered a memory leak when using the fantastic pyqtgraph.  A simple script to reproduce:

    import pyqtgraph as pg
    import sys
    import time
    pg.mkQApp()
    ww = pg.PlotWidget()
    for i in xrange(1000):
        w = pg.PlotWidget()
        w.close()
        w.destroy()
        print '.',
        sys.stdout.flush()
        time.sleep(0.01)

Since PlotWidget is a subclass of GraphicsView, I tried replacing creation of the former with the latter, which resulted with no leak, thus the problem must be in PlotWidget itself, or the `PlotItem` created by it.

Is there a way I can bypass it or a patch to apply?

Thanks,
shx2

Luke Campagnola

unread,
May 29, 2014, 10:37:32 PM5/29/14
to pyqt...@googlegroups.com
Confirmed. Can you tell me whether you are using PyQt or PySide? On PyQt, there seems to be a leak of Qt objects, even though no Python objects are being leaked (so this might be more of a PyQt bug, but I'll keep looking into it.)  On PySide, I see Python objects being leaked as well, so the fix would be different in that case.


Luke

shx2

unread,
May 31, 2014, 4:14:08 PM5/31/14
to pyqt...@googlegroups.com
I'm using PyQt. However, I do see python object being leaked (according to objgraph). There are some menu-related objects (e.g. QComboBox), and some weakref objects accumulating.

With some hackery, I managed to get to a state where there are no leaking python object, but still the memory kept inflating, so, as you suggest, it must also have something to do with the lower level.

sys.platform: linux2
sys.version: 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3]
qt bindings: PyQt4 4.9.1 Qt 4.8.1
pyqtgraph: 0.9.8; None
config:
{'antialias': False,
 'background': (0, 0, 0),
 'editorCommand': None,
 'enableExperimental': False,
 'exitCleanup': True,
 'foreground': (150, 150, 150),
 'leftButtonPan': True,
 'useOpenGL': False,
 'useWeave': True,
 'weaveDebug': False}

Luke Campagnola

unread,
May 31, 2014, 11:34:10 PM5/31/14
to pyqt...@googlegroups.com
On Sat, May 31, 2014 at 4:14 PM, shx2 <shx...@gmail.com> wrote:
I'm using PyQt. However, I do see python object being leaked (according to objgraph). There are some menu-related objects (e.g. QComboBox), and some weakref objects accumulating.

With some hackery, I managed to get to a state where there are no leaking python object,

All I needed to do was add `del w` to the example you posted above. Did you require more than that? (note that the current develop branch has a couple of cyclic-reference fixes, so we might expect 0.9.8 to leak more.)
 
but still the memory kept inflating, so, as you suggest, it must also have something to do with the lower level.

A workaround for this (that would also improve performance for this type of use case) is to defer the creation of the menu UI elements until the user requests the menu. In most cases, the menu would never need to be created at all. I have an older branch where I started work on this:  https://github.com/lcampagn/pyqtgraph/tree/deferred_menu

If you have an interest in completing that branch, I would be happy to provide assistance where needed. Otherwise, I might be able to get to this sometime in the next couple of weeks.


Luke

shx2

unread,
Jun 2, 2014, 1:02:32 AM6/2/14
to pyqt...@googlegroups.com
`del w` didn't help in my case, so I'm guessing the status in the development branch is better.

And absolutely, I'd like to help with the deffered menu support. I'll email you in a couple of days, when I find some free time for that, ok?
Reply all
Reply to author
Forward
0 new messages