Why is there a limit to 4MB heap?

I was just asked how much memory my ESP32 setup has/uses. I was pretty sure it’s 8MB. But a quick test gave this:

>>> import gc
>>> gc.mem_free()
>>> gc.mem_alloc()

I thus looked into the MP code and found this:

   switch (esp_spiram_get_chip_size()) {
        case ESP_SPIRAM_SIZE_16MBITS:
            mp_task_heap_size = 2 * 1024 * 1024;
        case ESP_SPIRAM_SIZE_32MBITS:
        case ESP_SPIRAM_SIZE_64MBITS:
            mp_task_heap_size = 4 * 1024 * 1024;

So even with 8MB RAM detected the heap is limited to 4MB. Is there a reason for that?

As far as I understand, spi-ram is limited to 4MB that can be directly mapped to the load/store address space.
Above that, you need to use a special himem API which is not supported on Micropython.



Thanks a lot. So it doesn’t make any difference if someone tries to run MP on a 4MB ESP32. Excellent.

Interesting; this reminds me of my STM32F746G discovery board. The SDRAM chip that is soldered is 16MB, but for some hardware-related reason only the first 8MB are addressible, thus there is essentially 8MB of wasted RAM.