rp2040: MemoryError: memory allocation failed, allocating 2072 bytes #12564
-
Direct question: is there a practical limit as to the size of a MP program ? Or that question phrased differently: does MP need a minimum of storage to be able to analyse / run a program and what is that minimum ? Background: when creating a by now sizable program of some 2000 lines (incl comments), I get an error message "MemoryError: memory allocation failed, allocating 2072 bytes". nb-1. I am aware that my program can be segmented, set_up as frozen code etc but understanding that there is a practical limit allows to stop figuring out if and where I my coding created this situation; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Interpreting Python requires memory, and the resulting bytecode requires memory too. There's no way around it. Step 1 is to pre-compile the code, i.e. upload as .mpy files instead of .py. They'll still be in RAM because littlefs doesn't support mapping to memory (AFAIK it cannot guarantee that the data is contiguous and most MCUs don't have a memory manager), but at least there's no more overhead for the interpreter. If that doesn't help enough, you need to freeze them into the MicroPython image, which requires that you build your own. The manual describes how to do that. |
Beta Was this translation helpful? Give feedback.
Interpreting Python requires memory, and the resulting bytecode requires memory too. There's no way around it.
Step 1 is to pre-compile the code, i.e. upload as .mpy files instead of .py. They'll still be in RAM because littlefs doesn't support mapping to memory (AFAIK it cannot guarantee that the data is contiguous and most MCUs don't have a memory manager), but at least there's no more overhead for the interpreter.
If that doesn't help enough, you need to freeze them into the MicroPython image, which requires that you build your own. The manual describes how to do that.