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()
1448816
>>> gc.mem_alloc()
2649536

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;
            break;
        case ESP_SPIRAM_SIZE_32MBITS:
        case ESP_SPIRAM_SIZE_64MBITS:
            mp_task_heap_size = 4 * 1024 * 1024;
            break;

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.

See:

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/himem.html

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.