Page 1 of 1

What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Mon Jan 22, 2018 9:32 am
by kkumar326
I'm getting this error: MemoryError: memory allocation failed, allocating 964 bytes
Now, I don't know how to approach for the fix as I don't really understand what's really happening here.
Please let me know about the error and what should I do to fix this? :idea:
Thanks a lot.

Re: What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Mon Jan 22, 2018 9:35 am
by stijn
There is only a limited amount of memory available. Due to fragmentation, or just because you are allocating a lot, you reached the limit and cannot allocate more. There have been numerous threads about this already, just search the forum for MemoryError.

Re: What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Mon Jan 22, 2018 9:46 am
by kkumar326
stijn wrote:
Mon Jan 22, 2018 9:35 am
There is only a limited amount of memory available. Due to fragmentation, or just because you are allocating a lot, you reached the limit and cannot allocate more. There have been numerous threads about this already, just search the forum for MemoryError.
I went through many posts but couldn't really find a solution. How exactly is micropython allocating memory here? What's "allocating 964 bytes"?

My boot.py file is running fine and giving proper output but when it goes to main.py, it throws this error. My main.py file is 9.4 kb and it's importing some libs, .mpy files and they have combined size of 7 kb. Is this error due to file sizes?

Re: What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Mon Jan 22, 2018 9:59 am
by stijn
How exactly is micropython allocating memory here?
There really is no way to give you an answer without having all code and running on the exact same platform you have. The rest is guesswork. Unless you have a stacktrace or so.
Is this error due to file sizes?
Again, impossible to tell. You can figure it out yourself, for example by adding/removing blocks of statements until the error occurs.

Re: What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Mon Jan 22, 2018 10:12 am
by kkumar326
There is no way to trace as it's not running at all. I think the issue is actually due to filesize which is getting bigger than available RAM.

This is just a guess though as I don't really know how files are interpreted in Micropython.

I'll follow your suggestion to add/remove blocks. It'd give some idea.

Thanks a lot.

PS: I'm running this on ESP8266 which I should've mentioned earlier.

Re: What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Mon Jan 22, 2018 9:45 pm
by Roberthh
The ESP8266 is good for about 300 lines Python code. If you have more, the only option is frozen bytecode.

Re: What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Tue Jan 23, 2018 4:29 am
by kkumar326
Will there be a difference in frozen modules and .mpy files?

Re: What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Tue Jan 23, 2018 6:36 am
by Roberthh
The difference is the time of compilation and the location of the bytecode for execution:

Python Source: -> compiled at runtime on the board -> placed in RAM
.mpy files: -> pre-compiled on the PC -> placed in RAM
Frozen bytecode: -> pre-compiled on the PC -> placed in Flash memory

Frozen bytecode stores the code and static data in the flash memory and uses RAM only for dynamic data. Still there is a limit for frozen bytecode - it must fit togehter with the firmware into the lower 1 MByte of the flash address range, but that is still a lot.

Re: What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Tue Jan 23, 2018 7:03 am
by kkumar326
1 MB is a lot. I'll build custom firmware with frozen modules then. That'll save me some RAM memory. Thanks a lot for this clear detail :D

Re: What's the meaning of this error: MemoryError: memory allocation failed, allocating 964 bytes?

Posted: Tue Jan 04, 2022 10:34 am
by KJM
I've got a 289 line .py file that won't run an esp8266 Robert (gives memory errors). I don't think I have the smarts to convert the .py to .mpy that the 8286 will accept. Is there some other way around this by converting the def functions to imports or something??