I am putting my script into flash using ampy. The script is about 1200 lines of python.
If I cut the script to the first 580 lines, it imports and mem_free shows 44288. If I put a few more lines, it fails to import
such as:
MemoryError: memory allocation failed, allocating %u bytes
Maybe by breaking the file up into files with less than 580 lines it can load and work with this little ram? Is this a limitation of micropython?
I believe this is also maybe a small bug that it prints %u to the console without formatting it.
ram needed during import far exceeds ram needed after
-
- Posts: 15
- Joined: Tue Feb 25, 2020 5:10 pm
Re: ram needed during import far exceeds ram needed after
Which platform are you using?
Which version of MicroPython?
Can you provide a transcript showing the problem and error message?
Which version of MicroPython?
Can you provide a transcript showing the problem and error message?
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: ram needed during import far exceeds ram needed after
This is a known behaviour and I'm sure it is documented somewhere.
If you put a normal .py file onto your microcontroller, it has to be compiled on the device. This needs lots of RAM and therefore you can run out of RAM during this process, even though after the compiling it looks like there is lots of RAM available.
The workaround is to use a precompiled .mpy file or freeze your script into the firmware.
If you put a normal .py file onto your microcontroller, it has to be compiled on the device. This needs lots of RAM and therefore you can run out of RAM during this process, even though after the compiling it looks like there is lots of RAM available.
The workaround is to use a precompiled .mpy file or freeze your script into 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
-
- Posts: 15
- Joined: Tue Feb 25, 2020 5:10 pm
Re: ram needed during import far exceeds ram needed after
I managed to break the program into 4 files and it can load now. This actually improved the code quality anyway.
Would it be possible to somehow use "flash as ram" like a swap space?
If I freeze the script in the firmware doesn't it need to be precompiled in this way? I managed to precompile a module on x86, but the recursive clone of esp32 toolchain is going to need a better connection.
Would it be possible to somehow use "flash as ram" like a swap space?
If I freeze the script in the firmware doesn't it need to be precompiled in this way? I managed to precompile a module on x86, but the recursive clone of esp32 toolchain is going to need a better connection.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: ram needed during import far exceeds ram needed after
Breaking large scripts into small modules is always a good idea and improves code quality and the effort needed to maintain it.
You can't use flash as RAM.
On the esp32 I precompile all my files using mpy-cross and then upload the .mpy using an ftp-server on the esp. Works well so far and I don't need to build a custom firmware.
For freezing into firmware you just need to copy the .py files into the modules directory specified in your board.manifest file (I think that's how it is handled now, I'm not up to date on custom firmwares anymore).
You can't use flash as RAM.
On the esp32 I precompile all my files using mpy-cross and then upload the .mpy using an ftp-server on the esp. Works well so far and I don't need to build a custom firmware.
For freezing into firmware you just need to copy the .py files into the modules directory specified in your board.manifest file (I think that's how it is handled now, I'm not up to date on custom firmwares anymore).
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