Remove imported module from RAM
Remove imported module from RAM
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?
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?
-
- Posts: 363
- Joined: Sun Apr 17, 2016 1:55 pm
Re: Remove imported module from RAM
just del it.
Re: Remove imported module from RAM
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
Thanks, I will try that.
Re: Remove imported module from RAM
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.
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
It should not leak, but it can worsen the memory fragmentation.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.
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.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Remove imported module from RAM
I'm having problems with unloading modules, the RAM does not get freed.
I'm using this function to delete a module:
I am importing a module and call one function. Then I delete it like that:
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.
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]
Code: Select all
from pysmartnode.helpers import helper
helper.help()
unloadModule(helper)
del helper
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Re: Remove imported module from RAM
Did you check sys.modules? Shouldn't you also unload pysmartnode.helpers and pysmartnode?
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Remove imported module from RAM
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).
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).
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Re: Remove imported module from RAM
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 )
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 )