MEmoryError: memory allocation failed
-
Re: [MemoryError: memory allocation failed](allocating 2048 bytes - LOPY - 1.2.0.b1)
Hello, I have this error too, but I want to know if this can work when the error comes out in the execution time, not the compilation, or how can I fix that.
Thanks in Advance
-
Depends.
2k on a LoPy is a lot of memory. If it occurs during compilation time, precompiling your code with mpy_cross is the solution.
Otherwise it is not that easy. You have to reduce memory fragmentation.
You can see the fragmentation withmicropython.mem_info()
Precompile as many files as you can. So the memory of the LoPy is not fragmented after boot time. Allocate huge buffers as early as you can. Reuse them instead of reallocate.You can try to call the garbage collector in front of your memory allocation. But this does not help in many situations. The garbage collector does not relocated objects in use, so the memory is still fragmented. If there is a background thread, it may fill your memory faster than you can free it. For example if it calculate float objects in a loop.