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

oliver oliver.schoenborn at gmail.com
Thu Sep 8 05:27:36 BST 2016


Correction: even Python modules cannot be unloaded, not even in Python 3.5.
Multiple discussions on the web indicate this, and a simple Python script
with a utility that shows memory consumption (like process explorer on
Windows) shows it. Here I tried with a pure-Python package called pytest:

import time, sys, gc

time.sleep(20)

# save modules catalog and import a big module
old_modules = sys.modules.copy()
import pytest # will cause 16 megs increase in process mem
print(sys.modules)
time.sleep(10)

# cleanup and restore original catalog to ensure that neither pytest or any
of the many mofulrd it
# imported are still referenced anywhere, and get gc to cleanup
del pytest
sys.modules = old_modules
del old_modules
gc.collect()   # mem stays at 16 megs more than original
print(sys.modules)
time.sleep(10)


Looks like there might be another attempt at supporting pure-Python module
unloading by developers of CPython but it is not clear when, and to what
extent it would work (you can imagine that modules would have to be marked
for how many times their symbols are referenced because two modules A and B
might import a common module C -- unloading A should not unload C if B is
present, but then unloading B after A unloaded should also unload C).

One post on python dev bug tracker site confirms that unloading a C/C++
extension (DLL) is near impossible.
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 Wed, Sep 7, 2016 at 9:48 PM, oliver <oliver.schoenborn at gmail.com> wrote:

>
>
> On Wed, Sep 7, 2016, 19:14 Andreas Pakulat <apaku at gmx.de> wrote:
>
>> Hi,
>>
>> On Wed, Sep 7, 2016 at 11:57 PM, Xavion <xavion.0 at gmail.com> wrote:
>> > So, you're both saying that the following two lines are equivalent
>> (from a
>> > memory footprint standpoint):
>> >
>> > from PyQt5.QtCore import QVariant
>> > from PyQt5 import QtCore
>> >
>> > In other words, in both cases, the whole of 'QtCore' will be imported
>> > (rather than just 'QVariant' in the first case).
>
>
> That is correct and same for all python packages/modules not just pyqt
>
> > Florian: I think you're saying that I can reduce the memory usage by
>> doing
>> > the following...
>> >
>> > from PyQt5 import QtCore
>> > mVariant = QtCore.QVariant
>> > del QtCore
>>
>
> Python modules can be unloaded this way but I don't think a DLL gets
> unloaded from memory once it is no longer in use by an app. But even if
> there was code in the Python interpreter to unload a DLL it would not get
> used because the above code will still be holding a reference to a symbol
> that is defined in the DLL so the DLL will not be considered unlovable.
>
> Why are you so concerned with memory occupied by modules?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160908/eb2dc68f/attachment.html>


More information about the PyQt mailing list