not logged in | [Login]

Control of uPy's memory manager (Garbage Collector, hence "gc") which (by default periodically) scans memory and releases chunks that are no longer in-use (aka "garbage").


import gc


gc.collect()    #run garbage collection now ???does it run asynchronously, or synchronously???
gc.disable()    #disable automatic periodic garbage collection (remember enable later!)
gc.enable()     #re-enable garbage collection
gc.mem_free()   #???return number of free bytes???
gc.mem_alloc()  #???return number of allocated bytes???


You may wish to disable automatic (periodic) garbage collection with gc.disable() during time critical operations.

Conversely purposefully calling gc.collect() post or during memory intensive operations and/or fast loops appears to prevent unexplained hard-crashes of the pyboard in certain circumstances.

Interrupt routines are not allowed to assign new memory (they can change existing memory allocations i.e. previously initialised variables) in case they 'interrupt' whilst the gc is collecting.

Memory management in uPy differs from CPython extensively due to requirement to run in a constrained (most commonly MCU) environment rather than the bloated environment normally available on a modern CPU/OS. [citation_needed]


CPython's garbage collector