Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allocate less micropython heap and make more space for IDF heap in ESP32-S2 port #7214

Closed
@tom-van

Description

@tom-van

In the current micropython git master we get following memory in an ordinary ESP32 without SPI RAM right after boot:

>>> esp32.idf_heap_info(esp32.HEAP_DATA)
[(7288, 176, 0, 176), (16648, 5608, 4096, 40), (94064, 5684, 4096, 5664), (15072, 13448, 8192, 13448), (113840, 112216, 65536, 112216)]
>>> micropython.mem_info()
stack: 704 out of 15360
GC: total: 64000, used: 1504, free: 62496
 No. of 1-blocks: 20, 2-blocks: 15, max blk sz: 18, max free sz: 3894

so we have 62496 bytes available for python, 137132 for C malloc()

The changed memory layout of ESP32-S2 changes the ratio strongly in favour of micropython heap:

>>> esp32.idf_heap_info(esp32.HEAP_DATA)
[(8148, 140, 0, 140), (197736, 38728, 32768, 34264), (14864, 13108, 8192, 13108)]
>>> micropython.mem_info()
stack: 704 out of 15360
GC: total: 128000, used: 2640, free: 125360
 No. of 1-blocks: 39, 2-blocks: 18, max blk sz: 32, max free sz: 7829

So on S2 we have 125360 bytes available for python and only 51976 bytes in IDF heaps for C malloc()
This gets exhausted just by connecting WiFi (STA mode with WPA2 PSK) and WebREPL request from network.

IMO we need to adjust mp_task_heap_size at ports/esp32/main.c:124

size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);

to better reflect the lower total amount of memory.
Something like

size_t mp_task_heap_size = MIN(heap_caps_get_largest_free_block(MALLOC_CAP_8BIT),
                                          heap_caps_get_total_size(MALLOC_CAP_8BIT) / 3);

It does not change allocation on ESP32. On ESP32-S2 it allocates 71872 for python/109464 free for malloc, it's enough for WebREPL and upip.install('logging') works too. idf_heap_info() shows minimal seen size 25868, so we have some margin.
Or use some other percentage of the total size?
What do you think?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions