Memory Allocation Failed + MemoryError - How do we garbage collected in Python?

Discussion related to "under the hood" OpenMV topics.
calvinmryan
Posts: 23
Joined: Tue Jan 30, 2018 11:05 am
Location: Boston, MA

Memory Allocation Failed + MemoryError - How do we garbage collected in Python?

Postby calvinmryan » Sat Mar 24, 2018 8:39 pm

We're consistently getting Memory Allocation Failed and MemoryErrors when we try to run our full code. When we remove different chunks of code we're able to run our code again. For example, If we remove chunk #2, chunk #1 runs and vice versa. So clearly our cod vve runs on it's own. Right now we're thinking that our code just isn't fitting into memory. Our code with comments is 24kB. Our code with comments stripped is 11kB. How can we confirm or deny this suspicion and if you don't think that's the case then how else can we resolve these memory errors?

On this note - how can we accomplish some type of garbage collection in micropython? We have a lot of stray variables and objects that we're creating that we don't see any apparent way to remove from memory after they're no longer needed? What are your thoughts?
User avatar
kwagyeman
Posts: 1771
Joined: Sun May 24, 2015 2:10 pm

Re: Memory Allocation Failed + MemoryError - How do we garbage collected in Python?

Postby kwagyeman » Sat Mar 24, 2018 10:13 pm

Hi,

http://docs.openmv.io/library/gc.html

... Not to be mean... but, it's like right there in the docs. :)_

Anyway, another trick is to pre-allocate memory buffers and avoid deleting them. Not sure if you've ever done this... but, as long as you don't re-create the variable each loop MicroPython doesn't have to re-alloc it.

...

Here's another trick for memory management with python. Once you're done with a variable call "del variable" on it. This marks if for garbage collection. You than then run the "gc" methods and the variable will get cleaned up. Otherwise, the variable only gets deleted when you go out of scope. Depending on the structure of your program this can take a while even if you used the variable a long time ago. So, del forces that out-of-scope process.

I've actually had to do this on the desktop too when really pushing python. I was processing 500 MB text files for some chip design stuff and I've run out of memory parsing a chip's netlist.
Nyamekye,
calvinmryan
Posts: 23
Joined: Tue Jan 30, 2018 11:05 am
Location: Boston, MA

Re: Memory Allocation Failed + MemoryError - How do we garbage collected in Python?

Postby calvinmryan » Sat Mar 24, 2018 10:58 pm

Wow, that's my bad haha. I did NOT check micropython docs this time around :/ But that thank you for the fast response! We're going to implement these tomorrow!

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests