8000 ports/esp32/main.c: Limit uPy heap size to a ratio of IDF heap size. · tom-van/micropython@07c1ef8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 07c1ef8

Browse files
committed
ports/esp32/main.c: Limit uPy heap size to a ratio of IDF heap size.
Before this change the largest free heap block was allocated for uPy heap. ESP32 S2 and S3 has different memory layout and this method results in so small remaining IDF heap that WiFi and other tasks breaks. The heuristic ratio was chosen as an usable compromise betwen uPy and IDF heap sizes for devices using WiFi and TLS and fine tuned to preserve original uPy heap size on ESP32 and IDF 4.0. Allocation on SPIRAM-equipped devices is not changed. Signed-off-by: Tomas Vanek <vanekt@fbl.cz>
1 parent b135e1c commit 07c1ef8

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

ports/esp32/main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,18 @@ void mp_task(void *pvParameter) {
127127
#endif
128128

129129
// If we have no SPIRAM,
130-
// allocate the uPy heap using malloc and get the largest available region
130+
// allocate the uPy heap using malloc
131131
if (!mp_task_heap_size) {
132-
mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
132+
size_t largest_free = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
133+
134+
// IDF 4.0 does not provide heap_caps_get_total_size(), use
135+
// total free instead as there is little memory allocated now
136+
size_t total_free = heap_caps_get_free_size(MALLOC_CAP_8BIT);
137+
138+
// Use a heuristic ratio to split uPy heap and the remaining IFD heap
139+
// or use the largest available region if smaller
140+
// Fine tuned to leave uPy heap 111168 bytes on ESP32 and IDF 4.0.2
141+
mp_task_heap_size = MIN(largest_free, total_free * 7 / 8 - 68249);
133142
mp_task_heap = malloc(mp_task_heap_size);
134143
}
135144

0 commit comments

Comments
 (0)
0