ESP32: Set maximum heap size in mpconfigport.h. #7963
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The problem: Too much memory allocated to heap on ESP32S2.
Micropython allocates too much RAM to the microython heap on ESP32S2 leaving insufficient RAM available for other IDF functions (causing panics in networking and espnow code). On the ESP32 101563 bytes is allocated to the heap by default. On the ESP32S2 this is ~120kB.
NOTE: when this this bug is resolved the default heap size on ESP32S2 will increase to ~160kB.
This fix: Set a maximum allowed value for the heap size.
Micropython currently allocates the largest available memory block to the micropython heap. This PR allows the maximum allowed heap size to be #defined as MICROPY_HEAP_SIZE_MAX. The default value is set to 104000 in mpconfigport.h and can be overridden in the board definition files: mpconfigboard.h.
With this default setting the espnow and wireless functions no longer generate panics on the ESP32S2.
Other approaches
#6785 allows the heap size to be set at run time (from NVS variables) which is a useful and powerful feature.
Nonetheless, it should be possible to deterministically set a reasonable maximum for the heap size"out of the box" in a compiled image that allows normal operation of an ESP32XX chip irregardless of exactly how the memory on the chip is fragmented.