Memory errors


There are two memory or RAM issues that you might encounter:

 

  1.  When your script is being converted from it's Python source into runnable bytecode, if the micro:bit runs out of RAM during this process, you get a MemoryError. This happens as the script runs (when you press reset).

    If you're seeing MemoryError immediately, then you likely have this error


  2. When your script is running, if you try to create variables or objects or anything else on the heap that exhausts the amount of available RAM, you get a MemoryError.

    If you're seeing MemoryError after some time, then it's likely this problem  


Workarounds

  • Shrinking variable names
  • Deleting comments
  • Using less whitespace.


The downside here is that this leaves your code difficult to read and debug.


For situation (2) you need to reduce the amount of memory you use while running the code

if you're comfortable with such tools, you could try using a minifier tool to shrink your code before putting on the microbit.



This article from the Micropython documentation also offers advice on working with constraints of micro-controllers. You also might find the advice useful in this Github thread.


 There are also few methods available in the gc and micropython modules to check the memory usage: