Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32: Micropython allocated heap is reduced for IDF >= 4.2.2. #7962

Closed
glenn20 opened this issue Nov 1, 2021 · 2 comments
Closed

ESP32: Micropython allocated heap is reduced for IDF >= 4.2.2. #7962

glenn20 opened this issue Nov 1, 2021 · 2 comments

Comments

@glenn20
Copy link
Contributor

glenn20 commented Nov 1, 2021

Micropython allocates a heap size of only 65536 bytes on the ESP32 for later versions of the ESP IDF (>= 4.2.2) instead of the actually available 110536 bytes.

Since the implementationof a new allocator in the IDF (October 2020), heap_cap_get_largest_block() returns the size of the largest allocatable block rounded down to the nearest power of 2 (in contradiction with the documentation.

I have filed an issue against the IDF at espressif/esp-idf#7808.

The follwing patch provides an ugly work around:

diff --git a/ports/esp32/main.c b/ports/esp32/main.c
index 2ba613668..dce82b7fb 100644
--- a/ports/esp32/main.c
+++ b/ports/esp32/main.c
@@ -131,7 +131,20 @@ void mp_task(void *pvParameter) {
     #else
     // Allocate the uPy heap using malloc and get the largest available region
     size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
-    void *mp_task_heap = malloc(mp_task_heap_size);
+    void *mp_task_heap;
+    for (size_t incr = mp_task_heap_size >> 1;
+         incr > 4;
+         incr >>= 1)
+    {
+        mp_task_heap_size += incr;
+        mp_task_heap = heap_caps_malloc(mp_task_heap_size, MALLOC_CAP_8BIT);
+        if (mp_task_heap == NULL) {
+            mp_task_heap_size -= incr;
+        }
+        heap_caps_free(mp_task_heap);
+        printf("HeapSize=0x%x  %d\n", mp_task_heap_size, mp_task_heap_size);
+    }
+    mp_task_heap = malloc(mp_task_heap_size);
     #endif
 
 soft_reset:
@IhorNehrutsa
Copy link
Contributor

Correspond to ESP32: RAM leak in ESP-IDF v4.3 #7813

@glenn20
Copy link
Contributor Author

glenn20 commented Nov 3, 2021

A fix for this bug is in progress from Espressif (and appears to have landed in 4.3-dev - thanks @IhorNehrutsa) and once it arrives the memory allocated to gc heap on ESP32S2 will increase from 128kB to 157kB on the GENERIC_S2 target - exacerbating the issues on ESP32S2.

I have submitted PR #7963 to set a configurable limit on the amount of RAM micropython allocates to the heap.

@glenn20 glenn20 closed this as completed May 1, 2023
tannewt pushed a commit to tannewt/circuitpython that referenced this issue May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants