MemoryError: memory allocation failed, allocating 1584 bytes

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mschulz
Posts: 35
Joined: Fri Apr 17, 2015 11:26 am
Location: Germany

MemoryError: memory allocation failed, allocating 1584 bytes

Post by mschulz » Wed Sep 16, 2015 12:48 pm

Hello,
I have a new Problem, that I do notunderstand.

I get the following error, when I write one more code line, but I think the line is ok. It is equal witch line I write:

Code: Select all

Traceback (most recent call last):
  File "main.py", line 23, in <module>
MemoryError: memory allocation failed, allocating 1584 bytes
Micro Python v1.3.10 on 2015-02-13; PYBv1.0 with STM32F405RG
Type "help()" for more information.

What can I do to solve this error?
Thank you!

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: MemoryError: memory allocation failed, allocating 1584 bytes

Post by danicampora » Wed Sep 16, 2015 12:54 pm

Sharing the piece of code that is causing the exception might be useful, otherwise it is difficult to help you...

mschulz
Posts: 35
Joined: Fri Apr 17, 2015 11:26 am
Location: Germany

Re: MemoryError: memory allocation failed, allocating 1584 bytes

Post by mschulz » Wed Sep 16, 2015 1:17 pm

This code runs, but when I uncommand the last line I get the error.

Code: Select all

                pos[0]=abs(F_KE_Rv/F_KE_Rh) # verhältnis Vorn zu Hinten
                if self.__pos_alt[0]>=0 and self.__pos_alt[2]>=0 and self.__pos_alt[4]>=0:
                    pos[0]=(self.__pos_alt[0]+self.__pos_alt[2]+self.__pos_alt[4]+pos[0])/4
                elif self.__pos_alt[0]>=0 and self.__pos_alt[2]>=0:
                    pos[0]=(self.__pos_alt[0]+self.__pos_alt[2]+pos[0])/3
                #elif self.__pos_alt[0]>=0:
                #   pos[0]=(self.__pos_alt[0]+pos[0])/2
               

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: MemoryError: memory allocation failed, allocating 1584 bytes

Post by pythoncoder » Thu Sep 17, 2015 6:23 am

The Pyboard has about 100KB of RAM for heap storage and it looks like your program is using all of it. Floating point maths uses the heap and my guess is that the line of maths you've commented out is trying to allocate memory which is unavailable. In other words the problem isn't with the line of code but with the overall design.

To answer the question of why all the memory is being used or to suggest a fix I think we'd need to see the entire program. Are your arrays large? If so, are they arrays or lists? (Arrays are more memory-efficient).
Peter Hinch
Index to my micropython libraries.

Post Reply