Replies: 8 comments 18 replies
-
One suggestion that has worked for me in the past is to break up into multiple smaller modules with judicious use of |
Beta Was this translation helpful? Give feedback.
-
I read somewhere, if you pack your code into the firmware, it will reduce the memory used. |
Beta Was this translation helpful? Give feedback.
-
So--called frozen bytecode moves the code from RAM to flash. It requires building the firmware yourself, but saves space and code startup time. |
Beta Was this translation helpful? Give feedback.
-
See here for details; The actual compilation instructions depend on what microcontroller you use: RP2040, ESP32, etc |
Beta Was this translation helpful? Give feedback.
-
oh, sorry , all I mentioned was refer to the size of .py files. |
Beta Was this translation helpful? Give feedback.
-
@glenn20 Can you do the native OTA on a 4MB device? (with Micropython firmware of cause). can't really find a full example for that case. |
Beta Was this translation helpful? Give feedback.
-
Another suggestion is to make sure that you're not doing unnecessary object creation in your imported module. Import the |
Beta Was this translation helpful? Give feedback.
-
Do you really need all the code? In a similar situation I used on-demand loading of module attributes, and even class attributes. But trhis doesn't help (and may actually cost more RAM) if you need everything. Are there parts of your code that are so unlikely to change that you could freeze them into the image? That really makes a difference! I have a docker to automate this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey, I have a huge .py file (70k bytes) with a lot of logic, and after adding some more code to it, it starts crashing with memory allocation errors. I'm currently compiling the .py to a .mpy before importing it.
Doing some tests I noticed that if I import the file, then run the garbage collector, and then check the allocated memory (before start running the code) it takes like 70k memory.
So my question is where should I direct my efforts to reduce the memory allocation when importing the file. Should I reduce global variables? Shouold I try to change the lists by tuples? Should I try to reduce characters on the names of functions and variables? Should I try to simply write more efficient logic thus reducing lines of code?
There are a lot of things I can think of that may reduce memory usage, but none seems too important, so, how should I face this? Or what is the best way to investigate what are the things that consumes the most memory when compiled and imported?
Note: I'm using micropython 20 with a generic esp32 board like the one below:

Edit: I want to specifically lower the amount of memory consumed when importing the file. I want to understand why it consumes so much memory, the .mpy file is half the size of the memory allocated when the file is imported, and variables and buffers created are not so many
Beta Was this translation helpful? Give feedback.
All reactions