Page 1 of 7

Remove imported module from RAM

Posted: Sat Nov 12, 2016 7:02 pm
by danielm
Is there any way how to remove imported module from RAM?

E.g. - it would make sense for untplib/NTPClient which is usually required to be used only once during boot process.

Class instantiated as an object can be deleted from RAM by "del" method?

Re: Remove imported module from RAM

Posted: Sun Nov 13, 2016 2:21 am
by shaoziyang
just del it.

Re: Remove imported module from RAM

Posted: Sun Nov 13, 2016 3:37 am
by dhylands
You'll get back most of the memory if you del the modules from sys.modules. There will still be things like strings which got interned which won't be freed back.

Re: Remove imported module from RAM

Posted: Sun Nov 13, 2016 6:35 pm
by danielm
Thanks, I will try that.

Re: Remove imported module from RAM

Posted: Sun Nov 13, 2016 8:20 pm
by markxr
As far as I've been able to tell, the memory is released if you delete it from sys.modules and also remove any other references to it (then call gc() if you want)

I didn't know about those "interned strings" though.

If we delete a module, then import it again later, does it leak? (I don't know the answer to this).

It seems reasonable to import a module, do some setup work (e.g. at reset), then when finished, delete the module to allow more ram for later usage.

Re: Remove imported module from RAM

Posted: Sun Nov 13, 2016 8:31 pm
by deshipu
markxr wrote: If we delete a module, then import it again later, does it leak? (I don't know the answer to this).

It seems reasonable to import a module, do some setup work (e.g. at reset), then when finished, delete the module to allow more ram for later usage.
It should not leak, but it can worsen the memory fragmentation.

Instead of deleting a module after your work with it is done, it seems to be much better to simply avoid public globals in your modules all together, and they only take up a little bit of space -- the rest is allocated dynamically as you use it.

Re: Remove imported module from RAM

Posted: Fri Jul 13, 2018 6:34 pm
by kevinkk525
I'm having problems with unloading modules, the RAM does not get freed.

I'm using this function to delete a module:

Code: Select all

def unloadModule(mod):
    # removes module from the system
    mod_name = mod.__name__
    if mod_name in sys.modules:
        del sys.modules[mod_name]
I am importing a module and call one function. Then I delete it like that:

Code: Select all

from pysmartnode.helpers import helper
helper.help()
unloadModule(helper)
del helper
But the RAM is only a small amount higher than with the module imported, far away from the free RAM before I imported the module.

Re: Remove imported module from RAM

Posted: Fri Jul 13, 2018 8:31 pm
by stijn
Did you check sys.modules? Shouldn't you also unload pysmartnode.helpers and pysmartnode?

Re: Remove imported module from RAM

Posted: Fri Jul 13, 2018 8:34 pm
by kevinkk525
it was not in sys.modules anymore and I don't have a reference to the module itself as I just call a function and delete the module.
pysmartnode and pysmartnode.helpers have been loaded before already and other modules in these packages are being used.

My goal is to outsource certain functions that are only needed once to different modules. Then I can just import these if they are necessary and unload them afterwards, freeing up some RAM that would otherwise be used by all the functions being in one module although only 20% might be used (it depends on user configuration, etc).

Re: Remove imported module from RAM

Posted: Sun Aug 05, 2018 10:36 am
by curlyz
Yea , I dont think it is that simple guys .
Interned string of that module will still exist in RAM

import micropython
micropython.qstr_info(1)

There exist every string , the memory that didnt get freed is here.
How to delete interned string may you ask , working on it :))