Memory consumption of bluetooth is enormous

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Re: Memory consumption of bluetooth is enormous

Postby rwel59 » Fri Jan 19, 2018 1:49 am

do you have any comments on what 'lowest possible values' might mean? or thoughts on a process to determine?
Last edited by rwel59 on Fri Jan 19, 2018 1:53 am, edited 1 time in total.

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Re: Memory consumption of bluetooth is enormous

Postby rwel59 » Fri Jan 19, 2018 1:50 am

any thoughts on what 'lowest possible values' might mean or how one might go about determining them?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Memory consumption of bluetooth is enormous

Postby ESP_Angus » Fri Jan 19, 2018 2:52 am

rwel59 wrote:any thoughts on what 'lowest possible values' might mean or how one might go about determining them?
Regarding this item:
chegewara wrote:If you are using wifi then in menuconfig->wifi start with the lowest settings, it gives another 30-ish kB (by default settings are setup very high)
Each of these numeric config parameters has a min/max value. In menuconfig, you can press "?" when any item is highlighted to view the help (which includes a description and the min/max range). Press Esc to exit the help. You can also read through the options here:
http://esp-idf.readthedocs.io/en/v3.0-r ... html#wi-fi

Setting the minimum for each value will reduce the memory usage (although for some items it's the worst-case memory usage not the initial memory usage). The documentation for each item will help you decide what is worth tweaking.

Note that setting minimum values will impact WiFi throughput depending on circumstances (again, the help gives some description as to each item). The defaults are chosen to be a tradeoff between performance and memory usage.
chegewara wrote:- if you dont use SSL menuconfig->mbedTLS->TLS maximum message content length is set to 16kB, why? set it to lowest possible, another 15kB
If you don't use SSL/TLS then you don't need to worry about this parameter at all, as it's only used if you create a TLS session.

The max message content length defaults to 16kB because TLS specifies a maximum 16kB message size. This is actually 32kB of RAM per TLS connection, as there are two buffers per connection - RX and TX.

If both ESP-IDF and the other end of your TLS connection is configured to support the TLS Maximum Fragment Length Negotiation Extension, then you can safely set this to a smaller value with no risk of problems (except maybe slower throughput if you move a lot of bulk data). However, most common cloud providers don't enable this extension on their TLS gateways.

Even if you can't use maximum fragment length negotiation, you can set the maximum message content length smaller if you know for certain that the other end of the connection will never try to send more than this many bytes at a single time. However if the other end does happen to send a TLS message which doesn't fit in the available RX buffer, the TLS connection will fail and need to be re-established.

In this case, the minimum size is usually determined by the usage in the TLS handshake phase. This depends on which TLS cipher is negotiated for the connection, which in turn depends on the mbedTLS configuration and the other end's TLS configuration. I suggest determining this experimentally, maybe start with 4096 bytes and increase/decrease. Remember to leave some headroom if there's a chance you'll need to switch ciphers in the future.

This is also summarised in the help for the config item: http://esp-idf.readthedocs.io/en/v3.0-r ... ontent-len

User avatar
hassan789
Posts: 156
Joined: Thu Jun 29, 2017 2:15 am

Re: Memory consumption of bluetooth is enormous

Postby hassan789 » Mon Jun 11, 2018 10:20 pm

ESP_Angus wrote:It is possible to release some of the RAM used by the Bluetooth stack if running with BLE only.

In IDF v2.1, this is done by setting "Release DRAM from Classic BT controller" under Component Config -> Bluetooth -> Bluedroid Bluetooth Stack.

In current master & IDF v3.0, this is done via esp_bt_controller_mem_release() function call: https://esp-idf.readthedocs.io/en/lates ... _bt_mode_t
Great tip! Run-time allocation is a life saver! This seems to be freeing ~70kB of heap.

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Memory consumption of bluetooth is enormous

Postby fly135 » Tue Jun 12, 2018 1:13 am

Holy Moly! Glad I saw this. I was struggling with 55K of internal mem. Now I have 140K!!! :D

John A

human890209
Posts: 54
Joined: Wed Aug 15, 2018 8:56 am

Re: Memory consumption of bluetooth is enormous

Postby human890209 » Tue Oct 16, 2018 6:25 am

Hi,
Thank for your advice. I'm a newbie of using BT of ESP32
I'm using BluetoothSerial, I don't know what the BT mode is, BLE or Classic?
I can only find

Code: Select all

esp_spp_init(ESP_SPP_MODE_CB)
in the library.
So can I release some memory for BluetoothSerial by using

Code: Select all

esp_bt_mem_release(mode)
or

Code: Select all

esp_bt_controller_mem_release
?

be-20-bb
Posts: 5
Joined: Sat Aug 31, 2019 10:15 am

Re: Memory consumption of bluetooth is enormous

Postby be-20-bb » Wed Dec 11, 2019 12:11 am

ESP_Angus wrote:
Fri Jan 19, 2018 2:52 am
The max message content length defaults to 16kB because TLS specifies a maximum 16kB message size. This is actually 32kB of RAM per TLS connection, as there are two buffers per connection - RX and TX.

If both ESP-IDF and the other end of your TLS connection is configured to support the TLS Maximum Fragment Length Negotiation Extension, then you can safely set this to a smaller value with no risk of problems (except maybe slower throughput if you move a lot of bulk data). However, most common cloud providers don't enable this extension on their TLS gateways.

Even if you can't use maximum fragment length negotiation, you can set the maximum message content length smaller if you know for certain that the other end of the connection will never try to send more than this many bytes at a single time. However if the other end does happen to send a TLS message which doesn't fit in the available RX buffer, the TLS connection will fail and need to be re-established.
Very informative, much appreciated. Thanks a lot!

harshal-vol
Posts: 1
Joined: Thu Jun 17, 2021 4:37 am

Re: Memory consumption of bluetooth is enormous

Postby harshal-vol » Thu Jun 17, 2021 5:11 am

Hi All,

We are developing an application using esp-mdf. In application we need BT only during provisioning, once it is done, we don't need BT and we would like to free up all the memory used by BT as we are running out of heap memory.

It seems BT is allocated 56K memory during compile time.

file=esp-idf/components/bt/bt.c

Code: Select all

343 /* the mode column will be modified by release function to indicate the available region */
 344 static btdm_dram_available_region_t btdm_dram_available_region[] = {
 345     //following is .data
 346     {ESP_BT_MODE_BTDM,          SOC_MEM_BT_DATA_START,      SOC_MEM_BT_DATA_END         },
 347     //following is memory which HW will use
 348     {ESP_BT_MODE_BTDM,          SOC_MEM_BT_EM_BTDM0_START,  SOC_MEM_BT_EM_BTDM0_END     },
 349     {ESP_BT_MODE_BLE,           SOC_MEM_BT_EM_BLE_START,    SOC_MEM_BT_EM_BLE_END      },
 350     {ESP_BT_MODE_BTDM,          SOC_MEM_BT_EM_BTDM1_START,  SOC_MEM_BT_EM_BTDM1_END     },
 351     {ESP_BT_MODE_CLASSIC_BT,    SOC_MEM_BT_EM_BREDR_START,  SOC_MEM_BT_EM_BREDR_REAL_END},
 352     //following is .bss
 353     {ESP_BT_MODE_BTDM,          SOC_MEM_BT_BSS_START,       SOC_MEM_BT_BSS_END          },
 354     {ESP_BT_MODE_BTDM,          SOC_MEM_BT_MISC_START,      SOC_MEM_BT_MISC_END         },
 355 };
Calling the following API does free up the dram regions but one chunk of memory (30k) is never freed.
esp_bt_mem_release(ESP_BT_MODE_BTDM);

Specifically, the following chunk is not freed.

Code: Select all

351     {ESP_BT_MODE_CLASSIC_BT,    SOC_MEM_BT_EM_BREDR_START,  SOC_MEM_BT_EM_BREDR_REAL_END},


And the reason is if condition at Line#1038:
  1. 1022 esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode)
  2. 1023 {
  3. 1024     bool update = true;
  4. 1025     intptr_t mem_start=(intptr_t) NULL, mem_end=(intptr_t) NULL;
  5. 1026
  6. 1027     if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
  7. 1028         return ESP_ERR_INVALID_STATE;
  8. 1029     }
  9. 1030
  10. 1031     //already released
  11. 1032     if (!(mode & btdm_dram_available_region[0].mode)) {
  12. 1033         return ESP_ERR_INVALID_STATE;
  13. 1034     }
  14. 1035
  15. 1036     for (int i = 0; i < sizeof(btdm_dram_available_region)/sizeof(btdm_dram_available_region_t); i++) {
  16. 1037         //skip the share mode, idle mode and other mode
  17. 1038         if (btdm_dram_available_region[i].mode == ESP_BT_MODE_IDLE
  18. 1039                 || (mode & btdm_dram_available_region[i].mode) != btdm_dram_available_region[i].mode) {
  19. 1040             //clear the bit of the mode which will be released
  20. 1041             btdm_dram_available_region[i].mode &= ~mode;
  21. 1042             continue;
  22. 1043         } else {
  23. 1044             //clear the bit of the mode which will be released
  24. 1045             btdm_dram_available_region[i].mode &= ~mode;
  25. 1046         }
For " ESP_BT_MODE_CLASSIC_BT ", the above piece of code, hits "//skip the share mode, idle mode and another mode".

Is there a way to free up that 30k chunk?
0x3ffb2730 - 0x3ffb6388 (???) = 3c58

As per the documentation, we should get about 70k memory but we are not able to get it.
esp-doc.JPG
esp-doc.JPG (92.32 KiB) Viewed 8693 times
Are we missing anything here? Is there a way to get all the BT memory?

BR.

ESP_xiongweichao
Posts: 4
Joined: Tue Jun 22, 2021 3:33 am

Re: Memory consumption of bluetooth is enormous

Postby ESP_xiongweichao » Tue Jun 22, 2021 3:48 am

Classic Bluetooth controller memory can be freed, but this means you can't use classic Bluetooth mode.
ebf80745-9c70-4a5d-ad9a-cd0afad28045.png
ebf80745-9c70-4a5d-ad9a-cd0afad28045.png (68.68 KiB) Viewed 8508 times
屏幕截图 2021-06-22 114320.png
屏幕截图 2021-06-22 114320.png (140.51 KiB) Viewed 8508 times

ESP_xiongweichao
Posts: 4
Joined: Tue Jun 22, 2021 3:33 am

Re: Memory consumption of bluetooth is enormous

Postby ESP_xiongweichao » Tue Jun 22, 2021 6:51 am

Classic Bluetooth controller memory can be freed, but this means you can't use classic Bluetooth mode.
ebf80745-9c70-4a5d-ad9a-cd0afad28045.png
ebf80745-9c70-4a5d-ad9a-cd0afad28045.png (68.68 KiB) Viewed 8518 times
Attachments
屏幕截图 2021-06-22 114320.png
屏幕截图 2021-06-22 114320.png (140.51 KiB) Viewed 8518 times

Who is online

Users browsing this forum: Baidu [Spider], ESP_ondrej and 177 guests