2

I'm using a library that doesn't free up all memory when the program is stopped. As a result I get an ENOMEM exception when restarting the program. Is there a way to free all RAM memory upon program restart?

For now I'm using hard-reset as a work-around, but I'd like to be able to just stop and restart the program and have it clean up the RAM at startup. Something like: clean_ram(). clean_ram() would force the garbage collector to free all allocated memory.

Bigman74066
  • 408
  • 4
  • 12

1 Answers1

0

The best way is to restart the MicroPython interpreter itself on the C level. Exit the Python program and do the whole interpreter setup again.

On Python level, you can manually "unimport" all modules and then call GC:

# main.py
# do not use any global imports here, except builtins
import gc, sys

while True:
    import program

    del program

    for key in sys.modules:
        del sys.modules[key]

    gc.collect()

then in program.py do whatever you wanted to do in main.

matejcik
  • 1,912
  • 16
  • 26