Remove imported module from RAM

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Remove imported module from RAM

Post by kevinkk525 » Fri Sep 21, 2018 7:41 pm

right, that is exactly what I am doing... Successfully with smaller modules but completely unsuccessfull with this one.
In manual test it only works if i remove "some" of "some.thing" too which does not work when the code runs by itself as part of my project (where I have absolutely no references to this module as it is loaded within a function and completely deleted just as in the manual tests).

I'm sorry to bother you with this and it's fine if you don't want to look into this. But if you do, please read my messages carefully.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Remove imported module from RAM

Post by jickster » Fri Sep 21, 2018 7:50 pm

kevinkk525 wrote:
Fri Sep 21, 2018 7:41 pm
right, that is exactly what I am doing... Successfully with smaller modules but completely unsuccessfull with this one.
In manual test it only works if i remove "some" of "some.thing" too which does not work when the code runs by itself as part of my project (where I have absolutely no references to this module as it is loaded within a function and completely deleted just as in the manual tests).

I'm sorry to bother you with this and it's fine if you don't want to look into this. But if you do, please read my messages carefully.
You want to remove a subset, from RAM, of the module you've imported?

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

Re: Remove imported module from RAM

Post by kevinkk525 » Fri Sep 21, 2018 7:53 pm

The module I imported
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Remove imported module from RAM

Post by jickster » Fri Sep 21, 2018 7:57 pm

kevinkk525 wrote:
Fri Sep 21, 2018 7:53 pm
The module I imported
Run this and reply with output

Code: Select all

import gc
import sys
gc.collect();gc.mem_free()
from pysmartnode.networking import mqtt_receive_config
gc.collect();gc.mem_free()
del mqtt_receive_config
del sys.modules["mqtt_receive_config"]
gc.collect();gc.mem_free()

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

Re: Remove imported module from RAM

Post by kevinkk525 » Fri Sep 21, 2018 8:04 pm

Code: Select all

>>> import sys
>>> import gc
>>> gc.collect();gc.mem_free()
22480
>>> from pysmartnode.networking import mqtt_receive_config
>>> gc.collect();gc.mem_free()
18112
>>> del mqtt_receive_config
>>> del sys.modules["mqtt_receive_config"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: mqtt_receive_config
>>> del sys.modules["pysmartnode.networking.mqtt_receive_config"]
>>> gc.collect();gc.mem_free()
17936
>>> del sys.modules["pysmartnode.networking"]
>>> gc.collect();gc.mem_free()
17952
>>> del sys.modules["pysmartnode"]
>>> gc.collect();gc.mem_free()
18400
>>> sys.modules
{'uasyncio': <module 'uasyncio'>, 'uasyncio.core': <module 'uasyncio.core'>, 'main': <module 'main'>}
>>>
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Remove imported module from RAM

Post by jickster » Fri Sep 21, 2018 8:06 pm

kevinkk525 wrote:
Fri Sep 21, 2018 8:04 pm

Code: Select all

>>> import sys
>>> import gc
>>> gc.collect();gc.mem_free()
22480
>>> from pysmartnode.networking import mqtt_receive_config
>>> gc.collect();gc.mem_free()
18112
>>> del mqtt_receive_config
>>> del sys.modules["mqtt_receive_config"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: mqtt_receive_config
>>> del sys.modules["pysmartnode.networking.mqtt_receive_config"]
>>> gc.collect();gc.mem_free()
17936
>>> del sys.modules["pysmartnode.networking"]
>>> gc.collect();gc.mem_free()
17952
>>> del sys.modules["pysmartnode"]
>>> gc.collect();gc.mem_free()
18400
>>> sys.modules
{'uasyncio': <module 'uasyncio'>, 'uasyncio.core': <module 'uasyncio.core'>, 'main': <module 'main'>}
>>>

Run

Code: Select all

dir()
sys.modules
import gc
import sys
gc.collect();gc.mem_free()
from pysmartnode.networking import mqtt_receive_config
dir()
sys.modules
gc.collect();gc.mem_free()

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

Re: Remove imported module from RAM

Post by kevinkk525 » Fri Sep 21, 2018 8:10 pm

Code: Select all

>>> dir()
['uos', 'gc', '__name__', 'main']
>>> import sys
>>> sys.modules
{'main': <module 'main'>}
>>> import gc
>>> gc.collect();gc.mem_free()
35520
>>> from pysmartnode.networking import mqtt_receive_config
>>> dir()
['uos', '__name__', 'main', 'gc', 'sys', 'mqtt_receive_config']
>>> sys.modules
{'uasyncio': <module 'uasyncio'>, 'uasyncio.core': <module 'uasyncio.core'>, 'pysmartnode.networking.mqtt_receive_config': <module 'pysmartnode.networking.mqtt_receive_config'>, 'pysmartnode.networking': <module 'pysmartnode.networking'>, 'main': <module 'main'>, 'pysmartnode': <module 'pysmartnode'>}
>>> gc.collect();gc.mem_free()
31200
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Remove imported module from RAM

Post by jickster » Fri Sep 21, 2018 8:16 pm

kevinkk525 wrote:
Fri Sep 21, 2018 8:10 pm

Code: Select all

>>> dir()
['uos', 'gc', '__name__', 'main']
>>> import sys
>>> sys.modules
{'main': <module 'main'>}
>>> import gc
>>> gc.collect();gc.mem_free()
35520
>>> from pysmartnode.networking import mqtt_receive_config
>>> dir()
['uos', '__name__', 'main', 'gc', 'sys', 'mqtt_receive_config']
>>> sys.modules
{'uasyncio': <module 'uasyncio'>, 'uasyncio.core': <module 'uasyncio.core'>, 'pysmartnode.networking.mqtt_receive_config': <module 'pysmartnode.networking.mqtt_receive_config'>, 'pysmartnode.networking': <module 'pysmartnode.networking'>, 'main': <module 'main'>, 'pysmartnode': <module 'pysmartnode'>}
>>> gc.collect();gc.mem_free()
31200
I don't think you can do what you want.

Functions in pysmartnode.networking could use items in pysmartnode.networking.mqtt_receive_config.

If you delete pysmartnode.networking.mqtt_receive_config, the virtual machine could crash if it tried to access things in pysmartnode.networking.mqtt_receive_config.

I'm not sure about this but it makes sense.

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

Re: Remove imported module from RAM

Post by kevinkk525 » Fri Sep 21, 2018 8:19 pm

even after deleting pysmartnode, pysmartnode.networking and pysmartnode.networking.mqtt_receive_config the VM worked perfectly fine in my program. (pysmartnode.networking is empty)
I opened a separete thread for this curiosity with some more explanations and observations: viewtopic.php?f=2&t=5288
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Remove imported module from RAM

Post by jickster » Fri Sep 21, 2018 8:21 pm

kevinkk525 wrote:
Fri Sep 21, 2018 8:19 pm
even after deleting pysmartnode, pysmartnode.networking and pysmartnode.networking.mqtt_receive_config the VM worked perfectly fine in my program. (pysmartnode.networking is empty)
I opened a separete thread for this curiosity with some more explanations and observations: viewtopic.php?f=2&t=5288

Not what I said
If you delete pysmartnode.networking.mqtt_receive_config, the virtual machine could crash if it tried to access things in pysmartnode.networking.mqtt_receive_config.
Therefore, it doesn't let you delete it.

Does it?

Run

Code: Select all

dir(); sys.modules
del mqtt_receive_config
del sys.modules["pysmartnode.networking.mqtt_receive_config"]
dir(); sys.modules

Post Reply