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 » Sun Sep 23, 2018 6:46 am

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Remove imported module from RAM

Post by pythoncoder » Sun Sep 23, 2018 8:03 am

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.
Peter Hinch
Index to my micropython libraries.

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

Re: Remove imported module from RAM

Post by kevinkk525 » Sun Sep 23, 2018 8:23 am

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.
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 » Sun Sep 23, 2018 2:14 pm

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

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

Re: Remove imported module from RAM

Post by kevinkk525 » Sun Sep 23, 2018 2:16 pm

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.
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 » Sun Sep 23, 2018 2:19 pm

Create a super basic .py like

Code: Select all

a = 1
b = a * 5
Freeze it using whatever process you use and paste the result here.


Sent from my iPhone using Tapatalk Pro

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

Re: Remove imported module from RAM

Post by kevinkk525 » Sun Sep 23, 2018 3:11 pm

what result?
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

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

Re: Remove imported module from RAM

Post by jickster » Sun Sep 23, 2018 3:39 pm

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

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

Re: Remove imported module from RAM

Post by kevinkk525 » Sun Sep 23, 2018 3:58 pm

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.
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 » Sun Sep 23, 2018 4:06 pm

What’s in frozen_mpy.c?


Sent from my iPhone using Tapatalk Pro

Post Reply