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

Xavion xavion.0 at gmail.com
Thu Sep 15 01:14:13 BST 2016


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160915/2165a8c5/attachment.html>


More information about the PyQt mailing list