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

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kkumar326
Posts: 16
Joined: Wed Nov 15, 2017 5:30 am

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

Post by kkumar326 » Mon Jan 22, 2018 9:32 am

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.

stijn
Posts: 507
Joined: Thu Apr 24, 2014 9:13 am

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

Post by stijn » 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.

kkumar326
Posts: 16
Joined: Wed Nov 15, 2017 5:30 am

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

Post by kkumar326 » Mon Jan 22, 2018 9:46 am

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?

stijn
Posts: 507
Joined: Thu Apr 24, 2014 9:13 am

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

Post by stijn » Mon Jan 22, 2018 9:59 am

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.

kkumar326
Posts: 16
Joined: Wed Nov 15, 2017 5:30 am

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

Post by kkumar326 » Mon Jan 22, 2018 10:12 am

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.

User avatar
Roberthh
Posts: 2236
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Mon Jan 22, 2018 9:45 pm

The ESP8266 is good for about 300 lines Python code. If you have more, the only option is frozen bytecode.

kkumar326
Posts: 16
Joined: Wed Nov 15, 2017 5:30 am

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

Post by kkumar326 » Tue Jan 23, 2018 4:29 am

Will there be a difference in frozen modules and .mpy files?

User avatar
Roberthh
Posts: 2236
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Tue Jan 23, 2018 6:36 am

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.

kkumar326
Posts: 16
Joined: Wed Nov 15, 2017 5:30 am

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

Post by kkumar326 » Tue Jan 23, 2018 7:03 am

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

Post Reply