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

oliver oliver.schoenborn at gmail.com
Thu Sep 8 05:26:03 BST 2016


Xavion,
The better way to save memory is to use following techniques:

   1. don't import more than necessary; for example, import from
   PyQt5.QtCore, not from PyQt5.Qt
   2. consider using Python slots (unrelated to Qt slots) for large numbers
   of objects stored in lists
   3. consider delaying loading of data (from disk or network), and only
   load what the app can show/process/edit
   4. use generators instead of lists by using range() and itertools module
   when possible, and consider creating your own generators using the yield
   statement
   5. *might* save some mem by using struct module where appropriate
   6. only create circular references when necessary (see
   http://engineering.hearsaysocial.com/2013/06/16/circular-references-in-python/),
   although it seems that python 3.4 is now quite good at deleting objects
   that were stuck in cyclical refs even if they have a del method (but the
   docs say that there can still be uncollectable garbage once in a while,
   without being specific, so if your app happens to generate a lot of objects
   with the particular kind of circular reference that is not collectable by
   interpreter, you would see steady increase in memory by your app as you
   create and delete those objects). Looks like objgraph might be a good
   utility for identifying cycles but I have not used it yet (just found out
   while researching this topic)


All that being said, you should probably dig deeper into into all of the
above only when you determine you have a memory leak or are using gigs more
memory than necessary.
Oliver


Oliver
Open Source contributions: PyPubSub <http://pubsub.sf.net/>, nose2pytest
<https://github.com/schollii/nose2pytest>, Lua-iCxx
<http://lua-icxx.sf.net/>, iof <http://iof.sf.net/>
StackOverflow <http://stackoverflow.com/users/869951/schollii> contributions


On Tue, Sep 6, 2016 at 12:22 AM, Xavion <xavion.0 at gmail.com> wrote:

> Hi All,
>
> I'm trying to cut down on the memory usage of my PyQt5 application.  I'm
> assuming that one way to do this is to only import the Qt submodules that
> are actually used.
>
> The three attached files illustrate my point.  The Qt5 Designer only
> imports the necessary submodules (e.g. "QtCore/QVariant").  Whereas, the
> PyQt5 UIC and RCC generators import the parent modules in full (e.g.
> "QtCore").
>
> Assuming my memory reduction theory is right, is there any way that I can
> get the UIC and RCC generators to behave like the Qt5 Designer?  There's
> probably no point in using this strategy throughout the rest of my code if
> not.
>
>
> --
> Regards, Xavion.
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160908/fad96de6/attachment.html>


More information about the PyQt mailing list