Disappearing Memory

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
samneggs
Posts: 20
Joined: Tue Jun 15, 2021 12:57 am

Disappearing Memory

Post by samneggs » Sun Feb 13, 2022 11:31 pm

What contributes to the memory used by a program? Not lists or arrays, just logic. And not normal Python - Assembly only.
This should be straight forward - one instruction per line, roughly 1200 lines.
To start with a get a baseline:

Code: Select all

import gc
print(gc.mem_free())
a=bytearray(180_000)
print(gc.mem_free())

results in:
187568
7552

So we start with 187,000 then reserve 180,000 and are left with 7500.
Makes sense.

Next add 1200 lines of assembly code in 12 routines, some repeating with a different name.
Of course my memory should be less with all that code but this is what I get:

Code: Select all

from micropython import const
import gc

print(gc.mem_free())
a=bytearray(110_000)
print(gc.mem_free())
...a bunch of assembly.....

results in:
183808
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
MemoryError: memory allocation failed, allocating 110000 bytes

So 1200 lines of assembly code with some overhead for 12 routines requires 70k?
Is this right?

Sam

samneggs
Posts: 20
Joined: Tue Jun 15, 2021 12:57 am

Re: Disappearing Memory

Post by samneggs » Fri Feb 18, 2022 8:29 pm

I think I figured out a workaround for my low memory problem but I'd still like to know more about how MicroPython allocates memory. My programs are typically one file that grows as I add functions until I run out of memory. For example, I type in many lines of text to Thonny and push run. Does this file get sent to the microcontroller in its entirety, stored in memory then converted to bytecode and run or converted on the fly, line by line? There seems to be more memory available if I use 'include' files and import functions at runtime.

I thought there may be a problem with memory fragmentation, so I put the big-ticket memory allocation (96k screen buffer) in the first line but it didn't seem to make much difference. If I get an out-of-memory error in the first line of the program, where is the memory reserved? It must be the code itself. I moved out some of that code onto the flash and imported it after the screen memory allocation and I get more memory back. It feels odd that it's the same program but I need to control how or when code is presented.
Sam

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Disappearing Memory

Post by deshipu » Fri Feb 18, 2022 10:17 pm

The code is read and compiled all at once from a single file. Dividing it into smaller modules may help. You can also cross-compile them into .mpy files, then they are already compiled, and importing them requires less memory.

Post Reply