|
| 1 | +6. Configuration options to optimize RAM and Flash |
| 2 | +================================================== |
| 3 | + |
| 4 | +6.1 Overview |
| 5 | +------------ |
| 6 | + |
| 7 | +There are several configuration options available to optimize Flash and RAM storage. |
| 8 | +The following list highlights key options that significantly increase the free DRAM, heap, and reduce the flash |
| 9 | +footprint. |
| 10 | + |
| 11 | +For more optimizations, we've also listed the reference links to esp-idf's optimization guide. |
| 12 | + |
| 13 | +6.2 Configurations |
| 14 | +------------------ |
| 15 | + |
| 16 | +6.2.1 Test Environment setup |
| 17 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 18 | + |
| 19 | +All numbers mentioned below are collected in the following environment: |
| 20 | + |
| 21 | +- esp-idf `v5.1.2`_ |
| 22 | +- esp-matter `cb3bc9d`_ |
| 23 | +- Example: `light`_ |
| 24 | +- SoC: ESP32-C3 |
| 25 | + |
| 26 | +.. note:: |
| 27 | + |
| 28 | + - These numbers may vary slightly in a different environment. |
| 29 | + - All numbers are in bytes |
| 30 | + - As we are using BLE only for commissioning, BLE memory is freed post commissioning, |
| 31 | + hence there is an increase in the free heap post commissioning. (``CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING=y``) |
| 32 | + - After building an example, some DRAM will be utilized, and the remaining DRAM will be |
| 33 | + allocated as heap. Therefore, a direct increase in the free DRAM will reflect as an increase in free heap. |
| 34 | + |
| 35 | + |
| 36 | +6.2.2 Default Configuration |
| 37 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 38 | + |
| 39 | +We have used the default light example here, and below listed are the static and dynamic sizes. |
| 40 | + |
| 41 | +.. csv-table:: Static memory stats |
| 42 | + :header: "", "Size", "Decreased by" |
| 43 | + |
| 44 | + Used D/IRAM,169042,-- |
| 45 | + Used Flash,1370786,-- |
| 46 | + |
| 47 | +.. csv-table:: Dynamic memory stats |
| 48 | + :header: "", "Free Heap", "Increased by" |
| 49 | + |
| 50 | + On Bootup, 64824, -- |
| 51 | + Post Commissioning, 126808, -- |
| 52 | + |
| 53 | + |
| 54 | +6.2.3 Disable the chip-shell |
| 55 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 56 | + |
| 57 | +Console shell is helpful when developing/debugging the application, but may not be necessary in |
| 58 | +production. Disabling the shell can save space. Disable the below configuration option. |
| 59 | + |
| 60 | +:: |
| 61 | + |
| 62 | + CONFIG_ENABLE_CHIP_SHELL=n |
| 63 | + |
| 64 | +.. csv-table:: Static memory stats |
| 65 | + :header: "", "Size", "Decreased by" |
| 66 | + |
| 67 | + Used D/IRAM,165480,3562 |
| 68 | + Used Flash,1311926,58860 |
| 69 | + |
| 70 | +.. csv-table:: Dynamic memory stats |
| 71 | + :header: "", "Free Heap", "Increased by" |
| 72 | + |
| 73 | + On Bootup, 76344, 11520 |
| 74 | + Post Commissioning, 137696, 10888 |
| 75 | + |
| 76 | + |
| 77 | +6.2.4 Adjust the dynamic endpoint count |
| 78 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 79 | + |
| 80 | +The default dynamic endpoint count is 16, which may be excessive for a normal application creating only 2 endpoints. |
| 81 | +eg: light, only has two endpoints, one for root endpoint and one for actual light. |
| 82 | +Adjusting this to a lower value, corresponding to the actual number of endpoints the application will create, can save DRAM. |
| 83 | + |
| 84 | +Here, we have set the dynamic endpoint count to 4. Increase in the DRAM per endpoint is ~275 bytes. |
| 85 | + |
| 86 | +:: |
| 87 | + |
| 88 | + CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT=4 |
| 89 | + |
| 90 | +.. csv-table:: Static memory stats |
| 91 | + :header: "", "Size", "Decreased by" |
| 92 | + |
| 93 | + Used D/IRAM,162136,3344 |
| 94 | + Used Flash,1311914,12 |
| 95 | + |
| 96 | +.. csv-table:: Dynamic memory stats |
| 97 | + :header: "", "Free Heap", "Increased by" |
| 98 | + |
| 99 | + On Bootup, 79688, 3344 |
| 100 | + Post Commissioning, 141204, 3508 |
| 101 | + |
| 102 | + |
| 103 | +6.2.5 Use the newlib nano formatting |
| 104 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 105 | + |
| 106 | +This optimization saves approximately 25-50K of flash, depending on the target. In our case, it results in a flash |
| 107 | +reduction of 61.5 KB. |
| 108 | + |
| 109 | +Additionally, it lowers the high watermark of task stack for functions that call printf() or other string formatting |
| 110 | +functions. Fore more details please take a look at esp-idf's `newlib nano formatting guide`_. |
| 111 | + |
| 112 | +:: |
| 113 | + |
| 114 | + CONFIG_NEWLIB_NANO_FORMAT=y |
| 115 | + |
| 116 | +.. csv-table:: Static memory stats |
| 117 | + :header: "", "Size", "Decreased by" |
| 118 | + |
| 119 | + Used D/IRAM,162136,0 |
| 120 | + Used Flash,1281354,30560 |
| 121 | + |
| 122 | +.. csv-table:: Dynamic memory stats |
| 123 | + :header: "", "Free Heap", "Increased by" |
| 124 | + |
| 125 | + On Bootup, 82748, 3060 |
| 126 | + Post Commissioning, 143956, 2752 |
| 127 | + |
| 128 | + |
| 129 | +6.2.6 Few BLE Optimizations |
| 130 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 131 | + |
| 132 | +Since most devices will primarily operate as BLE peripherals and typically won't need more than one connection |
| 133 | +(especially if it's just a Matter app), we can optimize by reducing the maximum allowed connections, thereby |
| 134 | +saving DRAM. Additionally, given the peripheral nature of these devices, we can disable the central and |
| 135 | +observer roles, for further optimization. |
| 136 | + |
| 137 | +Below are the configuration options that can be set to achieve these optimizations. |
| 138 | + |
| 139 | +:: |
| 140 | + |
| 141 | + CONFIG_NIMBLE_MAX_CONNECTIONS=1 |
| 142 | + CONFIG_BTDM_CTRL_BLE_MAX_CONN=1 |
| 143 | + CONFIG_BT_NIMBLE_ROLE_CENTRAL=n |
| 144 | + CONFIG_BT_NIMBLE_ROLE_OBSERVER=n |
| 145 | + |
| 146 | +.. csv-table:: Static memory stats |
| 147 | + :header: "", "Size", "Decreased by" |
| 148 | + |
| 149 | + Used D/IRAM,161682,454 |
| 150 | + Used Flash,1275860,5494 |
| 151 | + |
| 152 | +.. csv-table:: Dynamic memory stats |
| 153 | + :header: "", "Free Heap", "Increased by" |
| 154 | + |
| 155 | + On Bootup, 83220, 472 |
| 156 | + Post Commissioning, 143804, -152 |
| 157 | + |
| 158 | + |
| 159 | +6.2.7 Configuring logging event buffer |
| 160 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 161 | + |
| 162 | +Matter events serve as a historical record, stored in chronological order in the logging event buffer. |
| 163 | +By reducing the buffer size we can potentially save the DRAM. However, it's important to note that this reduction |
| 164 | +could lead to the omission of events. |
| 165 | + |
| 166 | +For instance, reducing the critical log buffer from 4K to 1K could save 3K DRAM, but it comes with the trade-off of |
| 167 | +potentially missing critical events. |
| 168 | + |
| 169 | +:: |
| 170 | + |
| 171 | + CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE=1024 |
| 172 | + |
| 173 | +.. csv-table:: Static memory stats |
| 174 | + :header: "", "Size", "Decreased by" |
| 175 | + |
| 176 | + Used D/IRAM,158610,3072 |
| 177 | + Used Flash,1275860,0 |
| 178 | + |
| 179 | +6.3 References for futher optimizations |
| 180 | +--------------------------------------- |
| 181 | + |
| 182 | +- `RAM optimization`_ |
| 183 | +- `Binary size optimization`_ |
| 184 | +- `Speed Optimization`_ |
| 185 | +- `ESP32 Memory Analysis — Case Study`_ |
| 186 | +- `Optimizing IRAM`_ can provide additional heap area but at the cost of execution speed. Relocating frequently-called |
| 187 | + functions from IRAM to flash may result in increased execution time |
| 188 | + |
| 189 | + |
| 190 | +.. _`v5.1.2`: https://github.com/espressif/esp-idf/tree/v5.1.2 |
| 191 | +.. _`cb3bc9d`: https://github.com/espressif/esp-matter/tree/cb3bc9d |
| 192 | +.. _`light`: https://github.com/espressif/esp-matter/tree/cb3bc9d/examples/light |
| 193 | +.. _`newlib nano formatting guide`: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-guides/performance/size.html#newlib-nano-formatting |
| 194 | +.. _`RAM optimization`: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-guides/performance/ram-usage.html |
| 195 | +.. _`Binary size optimization`: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-guides/performance/size.html |
| 196 | +.. _`Speed Optimization`: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-guides/performance/speed.html |
| 197 | +.. _`ESP32 Memory Analysis — Case Study`: https://blog.espressif.com/esp32-memory-analysis-case-study-eacc75fe5431 |
| 198 | +.. _`Optimizing IRAM`: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-guides/performance/ram-usage.html#optimizing-iram-usage |
0 commit comments