Garbage Collection Not Working

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
Posts: 1394
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Garbage Collection Not Working

Post by jimmo » Wed Jan 22, 2020 12:19 am

It's important for the port to provide a gc_collect() because it's responsible for figuring out how to extract root pointers from the stack and CPU registers (in a port-specific way).

When you say "I am trying to use MICROPY_ENABLE_GC" I assume this means you have actually enabled the GC.

To explain your earlier issue, what you're doing is essentially the same as

Code: Select all

a = [1]*1000
a = [2]*1000
It can't free the old data for "a" until after it has assigned the new value for a, so the peak memory is always 2000 items.

The solution is to either "soft reset" MicroPython in between scripts, or to change your code such that nothing is left in global scope (e.g. with a function as described). Or manually take things out of scope using "del a".

Post Reply