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

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?


… Not to be mean… but, it’s like right there in the docs. :slight_smile:_

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.

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