NimBLE-Arduino icon indicating copy to clipboard operation
NimBLE-Arduino copied to clipboard

Increased heap consumption after version 2.3.3

Open Laxilef opened this issue 2 months ago • 7 comments

Hi,

On the ESP32 S3 I see the free heap decrease immediately after calling BLEDevice::init("") by ~45kb, and then decrease further down to ~7kb:

Uptime: 2 d., 5 h., 56 min., 13 sec.
Heap: 43780 of 302148 bytes (min: 7420 bytes), max free block: 31732 bytes (min: 5108 bytes) 

In some cases I observed min heap & max free block of 100-200 bytes and then crash after a few hours.


The same project, but with library version 2.3.3:

Uptime: 2 d., 4 h., 50 min., 28 sec.
Heap: 55312 of 302212 bytes (min: 33636 bytes), max free block: 31732 bytes (min: 28660 bytes) 

I use pioarduino to build the project, release Arduino Release v3.3.1 based on ESP-IDF v5.5.1.

Laxilef avatar Oct 05 '25 23:10 Laxilef

Thanks, I will test this and see if I can reproduce it.

h2zero avatar Oct 15 '25 21:10 h2zero

Hi @h2zero,

Interesting observations: rolling back to kernel v3.2.1 based on ESP-IDF v5.4.2 solves the memory consumption problem. Could this be related to the addition of a native version of nimble to the kernel?

I tested with NimBLE-Arduino v2.3.6. On kernel v3.2.1, the minimum heap size never drops below 20 KB, but on v3.3.2, it drops to 200-500 bytes over time.

Laxilef avatar Oct 27 '25 21:10 Laxilef

Not sure yet, haven't had time to test this myself. Could you make a simple test project for me to repro this with so I can look into it?

h2zero avatar Oct 27 '25 21:10 h2zero

I see no issues with the s3 and 2.2.6/master and Arduino core v3.3.0, and with Arduino core v3.3.3 there is actually more free heap space, about 200 bytes, testing with the server example. I suspect something in the core has affected other areas of your project.

h2zero avatar Nov 01 '25 13:11 h2zero

Hi @h2zero,

Sorry it took me so long to respond. Yes, I also tested it on a simple sketch and didn't see any memory leaks.

But I haven't tested wifi+ble. The project using Nimble is large, and it's hard to immediately figure out what the problem might be, but I clearly see increased heap usage on the new kernel.

For now I'm trying to replace the web server with async, because I suspect the problem may be due to active use of wifi and simultaneous scanning of ble. What do you think about this?

Laxilef avatar Nov 02 '25 05:11 Laxilef

Async web server will make your problem worse, uses much more ram. I don't see any extra consumption with WiFi and ble.

h2zero avatar Nov 02 '25 14:11 h2zero

With ESPAsyncWebServer:

Build 	Env: s3_mini
Date: Nov 2 2025 12:13:10
Core: 3.3.2
SDK: v5.5.1-255-g07e9bf4970
Uptime 	0 d., 9 h., 26 min., 50 sec.
Free memory 	67628 of 304276 bytes (min: 22296 bytes)
max free block: 31732 bytes (min: 21492 bytes) 

To be honest, I didn't delve into the details of how the wifi/ble stack works, but it works more stable with the async web server. Perhaps the classic web server uses more CPU time and ble starts consuming more heap due to queues?

Laxilef avatar Nov 02 '25 18:11 Laxilef