Remove imported module from RAM

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
danielm
Posts: 133
Joined: Mon Oct 05, 2015 12:24 pm

Remove imported module from RAM

Post by danielm » Sat Nov 12, 2016 7:02 pm

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?

shaoziyang
Posts: 206
Joined: Sun Apr 17, 2016 1:55 pm

Re: Remove imported module from RAM

Post by shaoziyang » Sun Nov 13, 2016 2:21 am

just del it.

User avatar
dhylands
Posts: 2841
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Remove imported module from RAM

Post by dhylands » Sun Nov 13, 2016 3:37 am

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.

danielm
Posts: 133
Joined: Mon Oct 05, 2015 12:24 pm

Re: Remove imported module from RAM

Post by danielm » Sun Nov 13, 2016 6:35 pm

Thanks, I will try that.

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

Re: Remove imported module from RAM

Post by markxr » Sun Nov 13, 2016 8:20 pm

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.

User avatar
deshipu
Posts: 1328
Joined: Thu May 28, 2015 5:54 pm

Re: Remove imported module from RAM

Post by deshipu » Sun Nov 13, 2016 8:31 pm

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.

kevinkk525
Posts: 131
Joined: Sat Feb 03, 2018 7:02 pm

Re: Remove imported module from RAM

Post by kevinkk525 » Fri Jul 13, 2018 6:34 pm

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.

stijn
Posts: 260
Joined: Thu Apr 24, 2014 9:13 am

Re: Remove imported module from RAM

Post by stijn » Fri Jul 13, 2018 8:31 pm

Did you check sys.modules? Shouldn't you also unload pysmartnode.helpers and pysmartnode?

kevinkk525
Posts: 131
Joined: Sat Feb 03, 2018 7:02 pm

Re: Remove imported module from RAM

Post by kevinkk525 » Fri Jul 13, 2018 8:34 pm

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).

curlyz
Posts: 1
Joined: Sun Aug 05, 2018 10:31 am

Re: Remove imported module from RAM

Post by curlyz » Sun Aug 05, 2018 10:36 am

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 :))

Post Reply