memory allocation error

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
smhodge
Posts: 86
Joined: Tue Jan 22, 2019 2:16 am
Location: Kirkland, WA, USA

memory allocation error

Post by smhodge » Mon Mar 30, 2020 10:58 pm

So my code for a pyboard v1.1 has been growing bigger and bigger. I have a class defined that deals with external SPI flash memory, so naturally it has a lot of attributes and methods in it. Its code size is "gigantic" relative to all other classes I have defined for other uses. Today I added yet another method to this flash memory class and bingo I hit a "memory allocation error". I did various tests involving commenting out other methods in the class, enough to determine that the new code was perfectly ok and that it was simply a matter of what looks to me like "too much code" in the class. I suspect the issue is something to do with the heap.

My work-around was to simply move the new method outside the class, without changing the code except for various namespace tweaks. It works just fine. After doing this and then using mem_info() I get:

stack: 1140 out of 15360
GC: total: 103360, used: 53632, free: 49728
No. of 1-blocks: 505, 2-blocks: 76, max blk sz: 112, max free sz: 530

It seems like there is still lots of RAM available. So my question is more curiosity. Am I running into some under-the-hood limitations of microPython vis-a-vis classes and objects?

Thanks

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: memory allocation error

Post by dhylands » Mon Mar 30, 2020 11:06 pm

Your heap is probably fragmented. See this thread for more details: viewtopic.php?f=2&t=7915&p=45461&hilit=collect#p45461

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

Re: memory allocation error

Post by pythoncoder » Tue Mar 31, 2020 4:47 am

Also read this in the official docs.
Peter Hinch
Index to my micropython libraries.

pmulvey
Posts: 45
Joined: Sun Jul 29, 2018 8:12 am
Location: Athlone, Ireland

Re: memory allocation error

Post by pmulvey » Tue Mar 31, 2020 8:33 pm

Similar issue on the Esp8266, this is my (possible) solution:
viewtopic.php?f=2&t=7097&start=10#p45915
Paul Mulvey

Post Reply