Page 1 of 1

Wifi allocation failure

Posted: Sat May 25, 2019 12:44 pm
by permal
Hi,

I'm experiencing an issue where if there are more than three sockets actively communicating, the Wifi layer fails to allocate memory:
W (20948) wifi: alloc eb len=24 type=3 fail, heap:4107600
The result of this is that communication completely fails from this point; the device appears completely dead from a communication perspective, although it still remains connected to the access point. Further, there is no automatic recovery from this state without forcefully disconnecting the device from the access point via the AP's UI or possibly restarting the Wifi layer programatically.

This seems similar to, or possibly the same, problem for which Jason von Nieda filed ticket #2184 at GitHub and the related forum thread.

My current config (gist):
- CONFIG_SPIRAM_USE = Make RAM allocatable using malloc() as well
- mbedTLS uses SPIRAM, also tried "default alloc mode".
- CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL = 16384
- CONFIG_ESP32_WIFI_TX_BUFFER = Static (no dynamic option available)
- CONFIG_WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST = yes

Reducing CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL to 4096 made things better, but what other problems does that open up to? Also, the description for this item says:
Note that because FreeRTOS stacks are forced to internal memory, they will also use this memory pool; be sure to keep this in mind when adjusting this value.
What's is this text trying to say? That increasing this too much would prevent task stacks from being allocated?

I suppose the one thing I have to do is to free up internal memory, correct? As I've already moved mbedTLs to SPIRAM via config and enabled SPIRAM to be usable by malloc() (which I assume also includes C++ operator new?), the only thing left to adjust is CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL, right? I've lowered it to 1024 and no longer see the allocation failures even with six active sockets. I'm assuming there is a performance cost to this as a lot more memory access will have to go to SPIRAM instead, but an unstable device is unusable so the cost is worth it I think.

Edit: After additional testing, CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL made things better but I'm still seeing the allocation errors.

Is there anything else I can/should do?

Memory Allocation Failed on ESP32 S2 WROVER

Posted: Fri Nov 19, 2021 4:41 am
by Mathiyazhagan
Hi permal,
I am also facing a similar issue. I am using ESP32 S2 WROVER - 4MB for my project, Currently running 3 freeRTOS tasks stack size is 4096 each, and the ".bin" file size is 819KB. Able to build and flash then run the program on my device, but after some time (random time interval) it reboots or shows malloc failed in different modules. sometimes it shows in the wifi module, sometimes I2C and MQTT. then I enable CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS through menuconfig. Every time I run the program it shows Memory Allocation Failed in random time intervals on different modules. It seems dynamic allocation(malloc or calloc) is failed due to the lack of heap. In ESP32 almost all modules are using dynamic memory allocation. Please help me fix this issue.

Re: Wifi allocation failure

Posted: Fri Nov 19, 2021 11:47 am
by Steven65
Hello,

I am also encountering this issue. Don't know why its happening.