Memory release when a Plot is closed

357 views
Skip to first unread message

pytho...@gmail.com

unread,
Aug 21, 2016, 2:51:26 PM8/21/16
to pyqtgraph
Hi All,

My QApplication has a button (on the main window) which, when clicked, launches a pyqtgraph Plot. The code below shows how the plot is generated in a class.

self.win = pg.GraphicsWindow()
self.win.setWindowTitle(self.title)
self.p = self.win.addPlot()
self.curve = self.p.plot(self.Data1, pen='k')

One of the things I have noticed is that when I close the plot window in my application, the memory is not released. So for example, before the button is pressed, the Application takes around 20Mb. Once the plot is launched by clicking the button, this increases to 25Mb. But when I close the plot (by clicking on the x in the top right corner), the Application memory footprint stays at 25Mb. Is there any way to release this 5Mb of memory when the plot is closed (note that I have the line 'self.curve.clear()' so there is no leak while the plot is being updated in real-time).

Many thanks for reading!



Luke Campagnola

unread,
Aug 27, 2016, 3:15:27 AM8/27/16
to pyqt...@googlegroups.com
Probably the easiest thing to do is just manually clear the plot when the window is closed, by intercepting or overriding its closeEvent  (http://doc.qt.io/qt-4.8/qwidget.html#closeEvent). 

Ideally though, after you close the window all references to it would disappear and python would release any items displayed in the plot widget. There are many different reasons the widget or its items might still have references to it, though. I added the pyqtgraph.debug.ObjTracker class specifically to help track down leaked references; you can read the docstrings for that class to get an idea of how to use it. 

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/e26192b4-7729-4ddc-8891-444fa944d883%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pytho...@gmail.com

unread,
Aug 27, 2016, 5:54:57 AM8/27/16
to pyqtgraph
Hi Luke,

Many thanks for your reply.

I have tried to overload the closeEvent method but still there doesnt appear to be a release of the associated memory (according to Windows Task Manager). Please find below a minimal working example:

import pyqtgraph as pg
from PyQt4.QtGui import *
import FirstGUI_ui


class MainWindowGUI(QMainWindow, FirstGUI_ui.Ui_MainWindow):

   
def __init__(self):
       
QMainWindow.__init__(self)
       
self.setupUi(self)
       
self.Trend2.clicked.connect(lambda: self.trendGen())

   
def trendGen(self):
       
self.Plot1 = MyPlot()
       
self.Plot1.show()


class MyPlot(pg.PlotWidget):
   
def closeEvent(self, ev):
       
self.plotItem.clear()
       
self.plotItem = None
        ev.accept()
       
print 'Closed!'
        self = None


if __name__ == '__main__':
    app
= QApplication([])
    myGUI
= MainWindowGUI()
    myGUI
.show()
    app
.exec_()

Is there any issue in my definition of the closeEvent method?

Many thanks for your help!



On Saturday, 27 August 2016 08:15:27 UTC+1, Luke Campagnola wrote:
Probably the easiest thing to do is just manually clear the plot when the window is closed, by intercepting or overriding its closeEvent  (http://doc.qt.io/qt-4.8/qwidget.html#closeEvent). 

Ideally though, after you close the window all references to it would disappear and python would release any items displayed in the plot widget. There are many different reasons the widget or its items might still have references to it, though. I added the pyqtgraph.debug.ObjTracker class specifically to help track down leaked references; you can read the docstrings for that class to get an idea of how to use it. 
On Sun, Aug 21, 2016 at 11:51 AM, <pytho...@gmail.com> wrote:
Hi All,

My QApplication has a button (on the main window) which, when clicked, launches a pyqtgraph Plot. The code below shows how the plot is generated in a class.

self.win = pg.GraphicsWindow()
self.win.setWindowTitle(self.title)
self.p = self.win.addPlot()
self.curve = self.p.plot(self.Data1, pen='k')

One of the things I have noticed is that when I close the plot window in my application, the memory is not released. So for example, before the button is pressed, the Application takes around 20Mb. Once the plot is launched by clicking the button, this increases to 25Mb. But when I close the plot (by clicking on the x in the top right corner), the Application memory footprint stays at 25Mb. Is there any way to release this 5Mb of memory when the plot is closed (note that I have the line 'self.curve.clear()' so there is no leak while the plot is being updated in real-time).

Many thanks for reading!



--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages