[PyQt] Attempting to reduce the memory footprint of my PyQt5 application

Kovid Goyal kovid at kovidgoyal.net
Thu Sep 15 04:34:11 BST 2016


Use subprocess? Simpler code and no leaks.

subprocess.check_output([command] + args)

And also use python threads instead of QThread. In general, minimize the
use of Qt to only the parts it is actually needed for. Object lifetime
issues (leaks/segfaults) are very easy to trigger when using python+Qt.
So I long ago settled on the best practice of avoiding Qt as much
possible.

Kovid.

On Thu, Sep 15, 2016 at 10:14:13AM +1000, Xavion wrote:
> Okay, I've found the time to do some more testing on this.  Pympler wasn't
> able to find any memory leaks, but that could've just meant that I wasn't
> using it properly.  Either way, the good news is that I've managed to find
> the source of the leak manually.
> 
> Basically, I've got a QTimer which triggers the (re)starting of a QThread
> instance every 15 seconds.  Said thread creates a QProcess, which runs a
> shell command and collects its output.  The QProcess is then destroyed and
> the memory is (supposedly) freed.
> 
> However, despite this arrangement, the memory usage of my application still
> goes up by about 4 KiB each time the QThread is run.  While this may sound
> insignificant, bear in mind that said application is intended to run for
> days on end (in the system tray).  Over the period of a week, the memory
> increase would be roughly 160 MiB.  One of my users has reported seeing
> exactly this (and more, the longer it goes on).
> 
> No matter what I try, I can't seem to stop it from happening.  Even with
> the following code, the memory footprint still increases by the same amount
> each time the QThread is run.
> 
> pStatus = QtCore.QProcess()
> pStatus.start(sCommand, sArgs)
> pStatus.waitForStarted(500)
> while not pStatus.waitForFinished(500) :
> 
> pass
> 
> pStatus.deleteLater()
> gc.collect()
> self.exit()
> 
> 
> However, note that merely creating the QProcess (i.e. running the first
> line of code by itself) doesn't result in any ongoing increase in memory.
> This leads me to believe that the leak has something to do with the
> execution of the shell command.  Does anyone know how to make it go away?

> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt


-- 
_____________________________________

Dr. Kovid Goyal 
http://www.kovidgoyal.net
http://calibre-ebook.com
_____________________________________


More information about the PyQt mailing list