micropython icon indicating copy to clipboard operation
micropython copied to clipboard

MicroPython consumes too much memory and causes memory allocation error on V1 micro:bits

Open JackAtKitronik opened this issue 2 years ago • 5 comments

Describe the bug Using the micro:bit Python Editor, we were trying to add servo and LED functionality to the Kitronik MOVE Motor MicroPython library. Upon adding the servo code (about 20 lines of code) the code is now too big and when put on the V1 micro:bit it causes a memory allocation error saying memeory allocation failed, allocating 398 bytes.

To Reproduce Steps to reproduce the behavior:

  1. Open https://python.microbit.org/v/3/project
  2. Save KitronikMOVEMotor.py
  3. Add the code at MOVEMotor.py to main.py
  4. Download the code onto a V1 micro:bit
  5. The error memory error, memeory allocation failed, allocating 398 bytes appears

Expected behavior Following the same steps on as above but the program works on a V1 micro:bit. On a V2 micro:bit then the code works as expected.

micro:bit version V1 micro:bit

JackAtKitronik avatar Nov 04 '22 10:11 JackAtKitronik

I don't think there's much that can be done about this, the micro:bit V1 just doesn't have enough memory.

If you really need to get it working on a V1, it might be possible to optimise the code so that it does fit. But in general that's not possible to do, and the way to resolve it is either:

  1. have simpler programs
  2. use a micro:bit V2

dpgeorge avatar Nov 07 '22 05:11 dpgeorge

@JackAtKitronik was this the issue where the code worked fine in a single file but it was running out of memory (in this case by ~400 bytes) when using a module?

@dpgeorge if that's the case, would that be expected? that separating the code into a module could consume that much extra memory?

microbit-carlos avatar Nov 07 '22 18:11 microbit-carlos

I got this working and used it with a move:mini https://github.com/rhubarbdog/microbit-servo

rhubarbdog avatar Nov 07 '22 18:11 rhubarbdog

if that's the case, would that be expected? that separating the code into a module could consume that much extra memory?

Yes that's expected, simply because you now have two .py files: the main one which imports the second one. And some memory is already used by the first at the point where the second is imported and compiled.

This case would definitely benefit from being able to import .mpy files.

dpgeorge avatar Nov 07 '22 22:11 dpgeorge

@JackAtKitronik was this the issue where the code worked fine in a single file but it was running out of memory (in this case by ~400 bytes) when using a module?

@microbit-carlos almost. Putting it into a single file did make it smaller but not small enough to work.

I got this working and used it with a move:mini https://github.com/rhubarbdog/microbit-servo

@rhubarbdog great I'll have a look.

JackAtKitronik avatar Nov 08 '22 08:11 JackAtKitronik