5. gc module

Garbage Collector: A background process that runs in MicroPython to reclaim unused memory in the heap.
Normally, a collection is triggered only when a new allocation cannot be satisfied, i.e. on an out-of-memory condition.

gc.enable()

Enable automatic garbage collection.

from microbit import *
import gc

gc.enable()

gc.disable()

Disable automatic garbage collection. Heap memory can still be allocated, and garbage collection can still be initiated manually using gc.collect().

from microbit import *
import gc

gc.disable()

gc.isenabled()

Return True if garbage collection is enabled; False is disabled.

from microbit import *
import gc

print(gc.isenabled())
gc.disable()
print(gc.isenabled())
gc.enable()
print(gc.isenabled())

gc.collect()

Run a garbage collection.

from microbit import *
import gc

gc.collect()

gc.mem_alloc()

Return the number of bytes of heap RAM that are allocated.

from microbit import *
import gc

display.scroll(gc.mem_alloc())

gc.mem_free()

Return the number of bytes of available heap RAM, or -1 if this amount is not known.

from microbit import *
import gc

display.scroll(gc.mem_free())

gc.threshold([amount])
Set or query the additional GC allocation threshold.
A garbage collection will be triggered each time after the amount bytes have been allocated, since the previous time such an amount of bytes have been allocated.
amount is specified as less than the full heap size, (usually 64512), with the intention to trigger a collection earlier than when the heap becomes exhausted, and in the hope that an early collection will prevent excessive memory fragmentation.
Calling the function without argument will return the current value of the threshold.
A value of -1 means a disabled allocation threshold.
from microbit import *
import gc

gc.threshold(1024)
print(gc.threshold())