I've been working also with the A9 module, I was having some out-of-memory errors and decided to dive deeper.

So first, to understand better how memory works on Micropython and the heap/stack division, I suggest this video and this 2 pages that helped me a lot:

https://www.youtube.com/watch?v=d7qEzpnkWaY&t
https://www.adafruitdaily.com/2017/03/3 ... -mem_info/
https://docs.micropython.org/en/latest/ ... d.html#ram
You can check in your program how the memory is getting allocated using micropython.mem_info() from micropython module. Use gc.collect() from gc module to free blocks that are not been used anymore on the heap, you can see it working on code. Right now you will get something like this from mem_info():

stack: 664 out of 31744

GC: total: 3968, used: 1376, free: 2592

No. of 1-blocks: 4, 2-blocks: 3, max blk sz: 16, max free sz: 33

So the stack size is about 32000 but the heap size is just 3968, that's pretty low. For comparison, the A9 has 4 MB of SRAM, I have one esp32 that has only 520Kb and reads like this:

stack: 736 out of 15360

GC: total: 120064, used: 65888, free: 54176

No. of 1-blocks: 35, 2-blocks: 4, max blk sz: 1250, max free sz: 3281

So definitely there is something wrong. The most important value here is the total value on the GC: line, that's the heap memory size that is used for about every command we give in uPy.

From what I got to understand of this port, is that it uses the C++ CSDK to create the main thread that is always necessary (as pointed here

https://ai-thinker-open.github.io/GPRS_ ... -code.html) and than it creates a second thread that is the micropython thread. This threads is called MycroPyTask and it defines the stack and heap size, and also starts the gc. This all happens on main.c, basically on this part:

stack_top = info.stackTop + info.stackSize * 4;

mp_stack_set_top((void *)stack_top);

mp_stack_set_limit(MICROPYTHON_TASK_STACK_SIZE * 4 - 1024);

uint16_t heap_size;

heap = mp_allocate_heap(&heap_size);

gc_init(heap, heap + heap_size);

So checking there, the stack size is set for 2048 * 4 * 4 -1024, exactly the 31744 that appears at the stack size when we run mem_info().

Also, heap size is set as h_size = 4 * 1024, that is 3968 from the heap total +128bits that mem_info uses. I decided to play with these values, when changing stack size value I couldn't connect anymore to the module, tried some different values and went back to the one it is. Changing heap size values was a success, I've tried many different ones and the best I got was 59968, so almost 60k from what was 4k. I used h_size = 2048 * 30 for this. I've tested the available heap memory by allocating buffers and could use all this memory. Right now mem_info() gives me:

stack: 664 out of 31744

GC: total: 59968, used: 448, free: 59520

No. of 1-blocks: 11, 2-blocks: 5, max blk sz: 4, max free sz: 3713

so, no more out of memory problems. I believe we still can get higher values somehow, esp32 has only 1/8 of the A9 RAM and got about the double of the heap size that I got.

Quite a big text if any part is confusing/any doubt just ask me.