Remove imported module from RAM
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Remove imported module from RAM
All my modules are frozen bytecode otherwise I would have run out of RAM a long time ago. Functions, classes and objects still take RAM, strings not as they are loaded from flash (as long as I don't modify them).
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
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Remove imported module from RAM
I think the key concept is mutability. Mutable objects necessarily live in RAM. Python strings are immutable: when you modify them you create a new instance (this is Python, not just MicroPython). Classes and class instances are mutable: the language allows monkey-patching.
When I developed font_to_py.py I had to take some care to avoid inadvertent RAM use by a frozen module. For example as soon as you slice an immutable object you create a copy - unless you use a memoryview.
When I developed font_to_py.py I had to take some care to avoid inadvertent RAM use by a frozen module. For example as soon as you slice an immutable object you create a copy - unless you use a memoryview.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Remove imported module from RAM
My problem is, that I have many mutable objects like classes and class instances, which can not be changed to be immutable. Therefore I tried loading some of these only when needed and then unload them.
After importing the neccessary modules like uasyncio, mqtt_as, an mqtt_handler written by myself and some other small modules containing only classes and functions I have only 10.5k RAM left on a device with disabled filesystem (which offers ~4kB more RAM). After that it loads e.g. HTU21D class etc. That quickly lowers the free RAM to 5kB as some device drivers are not small either.
That's about context and motivation.
Therefore unloading modules not needed after startup was my current idea. But of course I tried to convert as much as possible to immutable objects (I probably missed some opportunities).
As far as I understand the font_to_py.py it's only usable for storing data as immutable objects that can be read by memoryview but I don't really need to store data except for configurations, which are immutable objects as they are strings frozen in bytecode that are not modified.
After importing the neccessary modules like uasyncio, mqtt_as, an mqtt_handler written by myself and some other small modules containing only classes and functions I have only 10.5k RAM left on a device with disabled filesystem (which offers ~4kB more RAM). After that it loads e.g. HTU21D class etc. That quickly lowers the free RAM to 5kB as some device drivers are not small either.
That's about context and motivation.
Therefore unloading modules not needed after startup was my current idea. But of course I tried to convert as much as possible to immutable objects (I probably missed some opportunities).
As far as I understand the font_to_py.py it's only usable for storing data as immutable objects that can be read by memoryview but I don't really need to store data except for configurations, which are immutable objects as they are strings frozen in bytecode that are not modified.
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
Just to be sure we’re on the same page: there’s a difference between FROZEN_STR and FROZEN_MPY.
Which one are you using?
Sent from my iPhone using Tapatalk Pro
Which one are you using?
Sent from my iPhone using Tapatalk Pro
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Remove imported module from RAM
Guess I don't know what you mean by FROZEN_STR.
My modules are frozen bytecode as part of the firmware. The strings of the configuartion are in these files and are not getting modified in any way and therefore stay in flash.
My modules are frozen bytecode as part of the firmware. The strings of the configuartion are in these files and are not getting modified in any way and therefore stay in flash.
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
Create a super basic .py like
Freeze it using whatever process you use and paste the result here.
Sent from my iPhone using Tapatalk Pro
Code: Select all
a = 1
b = a * 5
Sent from my iPhone using Tapatalk Pro
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Remove imported module from RAM
what result?
I put all my modules into ports/esp8266/modules folder and build the firmware..
I put all my modules into ports/esp8266/modules folder and build the firmware..
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
Which c file contains your frozen code?
Should be frozen.c for frozen STR and frozen_mpy.c for frozen mpy
Sent from my iPhone using Tapatalk Pro
Should be frozen.c for frozen STR and frozen_mpy.c for frozen mpy
Sent from my iPhone using Tapatalk Pro
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Remove imported module from RAM
As far as I can tell, no .c file holds my code. All files are inside ports/esp8266/build/frozen_mpy/ as .mpy
Apart from that there is code in ports/esp8266/build/build/frozen_mpy.o
There is nothing in ports/esp8266/build/build/frozen.o
I just followed the official instructions for freezing modules.
Apart from that there is code in ports/esp8266/build/build/frozen_mpy.o
There is nothing in ports/esp8266/build/build/frozen.o
I just followed the official instructions for freezing modules.
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
What’s in frozen_mpy.c?
Sent from my iPhone using Tapatalk Pro
Sent from my iPhone using Tapatalk Pro