diff --git a/tasmota/support_esp32.ino b/tasmota/support_esp32.ino index b7139bb61..38e8c908f 100644 --- a/tasmota/support_esp32.ino +++ b/tasmota/support_esp32.ino @@ -48,7 +48,26 @@ uint32_t ESP_getFreeHeap(void) { } uint32_t ESP_getMaxAllocHeap(void) { - return ESP.getMaxFreeBlockSize(); +/* + From libraries.rst + ESP.getMaxFreeBlockSize() returns the largest contiguous free RAM block in + the heap, useful for checking heap fragmentation. **NOTE:** Maximum + ``malloc()``able block will be smaller due to memory manager overheads. + + From HeapMetric.ino + ESP.getMaxFreeBlockSize() does not indicate the amount of memory that is + available for use in a single malloc call. It indicates the size of a + contiguous block of (raw) memory before the umm_malloc overhead is removed. + + It should also be pointed out that, if you allow for the needed overhead in + your malloc call, it could still fail in the general case. An IRQ handler + could have allocated memory between the time you call + ESP.getMaxFreeBlockSize() and your malloc call, reducing the available + memory. +*/ + uint32_t free_block_size = ESP.getMaxFreeBlockSize(); + if (free_block_size > 100) { free_block_size -= 100; } + return free_block_size; } void ESP_Restart(void) { @@ -273,7 +292,10 @@ uint32_t ESP_getFreeHeap(void) { } uint32_t ESP_getMaxAllocHeap(void) { - return ESP.getMaxAllocHeap(); + // largest block of heap that can be allocated at once + uint32_t free_block_size = ESP.getMaxAllocHeap(); + if (free_block_size > 100) { free_block_size -= 100; } + return free_block_size; } void ESP_Restart(void) {