size.rst 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. Minimizing Binary Size
  2. ======================
  3. :link_to_translation:`zh_CN:[中文]`
  4. {IDF_TARGET_REDUCED_BY_IRAM: default="DRAM", esp32="IRAM and/or DRAM (depending on sizes)"}
  5. The ESP-IDF build system compiles all source files in the project and ESP-IDF, but only functions and variables that are actually referenced by the program are linked into the final binary. In some cases, it is necessary to reduce the total size of the firmware binary, e.g., in order to fit it into the available flash partition size.
  6. The first step to reducing the total firmware binary size is measuring what is causing the size to increase.
  7. .. _idf.py-size:
  8. Measuring Static Sizes
  9. ----------------------
  10. To optimize both the firmware binary size and the memory usage, it is necessary to measure statically-allocated RAM (``data``, ``bss``), code (``text``), and read-only data (``rodata``) in your project.
  11. Using the :ref:`idf.py` sub-commands ``size``, ``size-components``, and ``size-files`` provides a summary of memory used by the project:
  12. .. note::
  13. It is possible to add ``-DOUTPUT_FORMAT=csv`` or ``-DOUTPUT_FORMAT=json`` to get the output in CSV or JSON format.
  14. Size Summary ``idf.py size``
  15. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  16. .. only:: esp32
  17. .. code-block:: bash
  18. $ idf.py size
  19. [...]
  20. Total sizes:
  21. Used static DRAM: 10608 bytes ( 170128 remain, 5.9% used)
  22. .data size: 8464 bytes
  23. .bss size: 2144 bytes
  24. Used static IRAM: 48834 bytes ( 82238 remain, 37.3% used)
  25. .text size: 47807 bytes
  26. .vectors size: 1027 bytes
  27. Used Flash size : 117391 bytes
  28. .text: 80103 bytes
  29. .rodata: 37032 bytes
  30. Total image size: 174689 bytes (.bin may be padded larger)
  31. .. only:: not esp32
  32. .. code-block:: bash
  33. $ idf.py size
  34. [...]
  35. Total sizes:
  36. Used stat D/IRAM: 53743 bytes ( 122385 remain, 30.5% used)
  37. .data size: 6504 bytes
  38. .bss size: 1984 bytes
  39. .text size: 44228 bytes
  40. .vectors size: 1027 bytes
  41. Used Flash size : 118879 bytes
  42. .text: 83467 bytes
  43. .rodata: 35156 bytes
  44. Total image size: 170638 bytes (.bin may be padded larger)
  45. This output breaks down the size of all static memory regions in the firmware binary:
  46. .. only:: esp32
  47. .. code-block:: bash
  48. $ idf.py size
  49. [...]
  50. Total sizes:
  51. Used static DRAM: 10608 bytes ( 170128 remain, 5.9% used)
  52. .data size: 8464 bytes
  53. .bss size: 2144 bytes
  54. Used static IRAM: 48834 bytes ( 82238 remain, 37.3% used)
  55. .text size: 47807 bytes
  56. .vectors size: 1027 bytes
  57. Used Flash size : 117391 bytes
  58. .text: 80103 bytes
  59. .rodata: 37032 bytes
  60. Total image size: 174689 bytes (.bin may be padded larger)
  61. - ``Used static DRAM``: Total amount of DRAM allocated at compile time. ``remain`` indicates the amount of DRAM left to be used as heap memory at runtime. Note that due to meta data overhead, implementation constraints, and startup heap allocations, the actual size of the DRAM heap is smaller.
  62. - ``.data size``: Amount of DRAM allocated at compile time for the ``.data`` (i.e., all statically allocated variables that are initialized to non-zero values). ``.data`` also consumes space in the binary image to store the non-zero initialization values.
  63. - ``.bss size``: Amount of DRAM allocated at compile time for ``.bss`` (i.e., all statically allocated variables that are initialized to zero). ``.bss`` does not consume extra space in flash.
  64. - ``Used static IRAM``: Total amount of IRAM allocated at compile time. ``remain`` indicates the amount of IRAM left to be used as heap memory at runtime. Note that due to meta data overhead, implementation constraints, and startup heap allocations, the actual size of the IRAM heap is smaller.
  65. - ``.text size``: Amount of IRAM used for ``.text`` (i.e., all code that is executed from :ref:`IRAM <iram>`). ``.text`` also consumes space in the binary image as the code is initially stored there and is then copied over to IRAM on startup.
  66. - ``Used Flash size``: Total amount of flash used (excluding usage by DRAM and IRAM)
  67. - ``.text``: Amount of flash used for ``.text`` (i.e., all code that is executed via the flash cache, see :ref:`IROM <irom>`).
  68. - ``.rodata``: Amount of flash used for ``.rodata`` (i.e., read-only data that is loaded via the flash cache, see :ref:`DROM <drom>`).
  69. - ``Total image size`` is the estimated total size of the binary file.
  70. .. only:: not esp32
  71. .. code-block:: bash
  72. $ idf.py size
  73. [...]
  74. Total sizes:
  75. Used stat D/IRAM: 53743 bytes ( 122385 remain, 30.5% used)
  76. .data size: 6504 bytes
  77. .bss size: 1984 bytes
  78. .text size: 44228 bytes
  79. .vectors size: 1027 bytes
  80. Used Flash size : 118879 bytes
  81. .text: 83467 bytes
  82. .rodata: 35156 bytes
  83. Total image size: 170638 bytes (.bin may be padded larger)
  84. - ``Used stat D/IRAM``: Total amount of D/IRAM used at compile time. ``remain`` indicates the amount of D/IRAM left to be used as heap memory at runtime. Note that due to meta data overhead, implementation constraints, and startup heap allocations, the actual size of the DRAM heap is smaller.
  85. - ``.data size``: Amount of D/IRAM allocated at compile time for the ``.data`` (i.e., all statically allocated variables that are initialized to non-zero values). ``.data`` also consumes space in the binary image to store the non-zero initialization values.
  86. - ``.bss size``: Amount of D/IRAM allocated at compile time for ``.bss`` (i.e., all statically allocated variables that are initialized to zero). ``.bss`` does not consume extra space in flash.
  87. - ``.text size``: Amount of D/IRAM used for ``.text`` (i.e., all code that is executed from internal RAM). ``.text`` also consumes space in the binary image as the code is initially stored there and is then copied over to D/IRAM on startup.
  88. - ``Used Flash size``: Total amount of flash used (excluding usage by D/IRAM)
  89. - ``.text``: Amount of flash used for ``.text`` (i.e., all code that is executed via the flash cache, see :ref:`IROM <irom>`).
  90. - ``.rodata``: Amount of flash used for ``.rodata`` (i.e., read-only data that is loaded via the flash cache, see :ref:`DROM <drom>`).
  91. - ``Total image size`` is the estimated total size of the binary file.
  92. Component Usage Summary ``idf.py size-components``
  93. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  94. The summary output provided by ``idf.py size`` does not give enough details to find the main contributor to excessive binary size. To analyze in detail, use ``idf.py size-components``.
  95. .. code-block:: bash
  96. $ idf.py size-components
  97. [...]
  98. Total sizes:
  99. DRAM .data size: 14956 bytes
  100. DRAM .bss size: 15808 bytes
  101. Used static DRAM: 30764 bytes ( 149972 available, 17.0% used)
  102. Used static IRAM: 83918 bytes ( 47154 available, 64.0% used)
  103. Flash code: 559943 bytes
  104. Flash rodata: 176736 bytes
  105. Total image size:~ 835553 bytes (.bin may be padded larger)
  106. Per-archive contributions to ELF file:
  107. Archive File DRAM .data & .bss & other IRAM D/IRAM Flash code & rodata Total
  108. libnet80211.a 1267 6044 0 5490 0 107445 18484 138730
  109. liblwip.a 21 3838 0 0 0 97465 16116 117440
  110. libmbedtls.a 60 524 0 0 0 27655 69907 98146
  111. libmbedcrypto.a 64 81 0 30 0 76645 11661 88481
  112. libpp.a 2427 1292 0 20851 0 37208 4708 66486
  113. libc.a 4 0 0 0 0 57056 6455 63515
  114. libphy.a 1439 715 0 7798 0 33074 0 43026
  115. libwpa_supplicant.a 12 848 0 0 0 35505 1446 37811
  116. libfreertos.a 3104 740 0 15711 0 367 4228 24150
  117. libnvs_flash.a 0 24 0 0 0 14347 2924 17295
  118. libspi_flash.a 1562 294 0 8851 0 1840 1913 14460
  119. libesp_system.a 245 206 0 3078 0 5990 3817 13336
  120. libesp-tls.a 0 4 0 0 0 5637 3524 9165
  121. [... removed some lines here ...]
  122. libesp_rom.a 0 0 0 112 0 0 0 112
  123. libcxx.a 0 0 0 0 0 47 0 47
  124. (exe) 0 0 0 3 0 3 12 18
  125. libesp_pm.a 0 0 0 0 0 8 0 8
  126. libesp_eth.a 0 0 0 0 0 0 0 0
  127. libmesh.a 0 0 0 0 0 0 0 0
  128. The first lines of the output from ``idf.py size-components`` are the same as that from ``idf.py size``. After this, a table is printed as ``Per-archive contributions to ELF file``. This means how much each static library archive has contributed to the final binary size.
  129. Generally, one static library archive is built per component, although some are binary libraries included by a particular component, for example, ``libnet80211.a`` is included by ``esp_wifi`` component. There are also toolchain libraries such as ``libc.a`` and ``libgcc.a`` listed here, these provide Standard C/C++ Library and toolchain built-in functionality.
  130. If your project is simple and only has a ``main`` component, then all of the project's code will be shown under ``libmain.a``. If your project includes its own components (see :doc:`/api-guides/build-system`), then they will each be shown on a separate line.
  131. The table is sorted in descending order of the total contribution of the static archive to the binary size.
  132. The columns are as follows:
  133. .. list::
  134. - ``DRAM .data & .bss & other`` - ``.data`` and ``.bss`` are the same as for the totals shown above. Both are static variables and reduce the total available RAM at runtime, but ``.bss`` does not contribute to the binary file size. ``other`` is a column for any custom section types that also contribute to RAM size. Usually, the value is 0.
  135. :esp32: - ``IRAM`` - is the same as for the totals shown above. It refers to code linked to execute from IRAM, which uses space in the binary file and also reduces IRAM that can be dynamically allocated at runtime using ``HEAP_CAP_32BIT``.
  136. :esp32: - ``D/IRAM`` - shows IRAM space which, due to occupying D/IRAM space, is also reducing available DRAM available as heap at runtime.
  137. :not esp32: - ``IRAM`` - is the same as for the totals shown above. It refers to code linked to execute from IRAM, which uses space in the binary file and also reduces DRAM available as heap at runtime.
  138. - ``Flash code & rodata`` - these are the same as the totals above, IROM and DROM space accessed from the flash cache that contribute to the binary size.
  139. Source File Usage Summary ``idf.py size-files``
  140. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  141. For even more details, run ``idf.py size-files`` to get a summary of the contribution each object file has made to the final binary size. Each object file corresponds to a single source file.
  142. .. code-block:: bash
  143. $ idf.py size-files
  144. [...]
  145. Total sizes:
  146. DRAM .data size: 14956 bytes
  147. DRAM .bss size: 15808 bytes
  148. Used static DRAM: 30764 bytes ( 149972 available, 17.0% used)
  149. Used static IRAM: 83918 bytes ( 47154 available, 64.0% used)
  150. Flash code: 559943 bytes
  151. Flash rodata: 176736 bytes
  152. Total image size:~ 835553 bytes (.bin may be padded larger)
  153. Per-file contributions to ELF file:
  154. Object File DRAM .data & .bss & other IRAM D/IRAM Flash code & rodata Total
  155. x509_crt_bundle.S.o 0 0 0 0 0 0 64212 64212
  156. wl_cnx.o 2 3183 0 221 0 13119 3286 19811
  157. phy_chip_v7.o 721 614 0 1642 0 16820 0 19797
  158. ieee80211_ioctl.o 740 96 0 437 0 15325 2627 19225
  159. pp.o 1142 45 0 8871 0 5030 537 15625
  160. ieee80211_output.o 2 20 0 2118 0 11617 914 14671
  161. ieee80211_sta.o 1 41 0 1498 0 10858 2218 14616
  162. lib_a-vfprintf.o 0 0 0 0 0 13829 752 14581
  163. lib_a-svfprintf.o 0 0 0 0 0 13251 752 14003
  164. ssl_tls.c.o 60 0 0 0 0 12769 463 13292
  165. sockets.c.o 0 648 0 0 0 11096 1030 12774
  166. nd6.c.o 8 932 0 0 0 11515 314 12769
  167. phy_chip_v7_cal.o 477 53 0 3499 0 8561 0 12590
  168. pm.o 32 364 0 2673 0 7788 782 11639
  169. ieee80211_scan.o 18 288 0 0 0 8889 1921 11116
  170. lib_a-svfiprintf.o 0 0 0 0 0 9654 1206 10860
  171. lib_a-vfiprintf.o 0 0 0 0 0 10069 734 10803
  172. ieee80211_ht.o 0 4 0 1186 0 8628 898 10716
  173. phy_chip_v7_ana.o 241 48 0 2657 0 7677 0 10623
  174. bignum.c.o 0 4 0 0 0 9652 752 10408
  175. tcp_in.c.o 0 52 0 0 0 8750 1282 10084
  176. trc.o 664 88 0 1726 0 6245 1108 9831
  177. tasks.c.o 8 704 0 7594 0 0 1475 9781
  178. ecp_curves.c.o 28 0 0 0 0 7384 2325 9737
  179. ecp.c.o 0 64 0 0 0 8864 286 9214
  180. ieee80211_hostap.o 1 41 0 0 0 8578 585 9205
  181. wdev.o 121 125 0 4499 0 3684 580 9009
  182. tcp_out.c.o 0 0 0 0 0 5686 2161 7847
  183. tcp.c.o 2 26 0 0 0 6161 1617 7806
  184. ieee80211_input.o 0 0 0 0 0 6797 973 7770
  185. wpa.c.o 0 656 0 0 0 6828 55 7539
  186. [... additional lines removed ...]
  187. After the summary of total sizes, a table of ``Per-file contributions to ELF file`` is printed.
  188. The columns are the same as shown above for ``idy.py size-components``, but this time the granularity is the contribution of each individual object file to the binary size.
  189. For example, we can see that the file ``x509_crt_bundle.S.o`` contributed 64,212 bytes to the total firmware size, all as ``.rodata`` in flash. Therefore we can guess that this application is using the :doc:`/api-reference/protocols/esp_crt_bundle` feature and not using this feature would save at last this many bytes from the firmware size.
  190. Some of the object files are linked from binary libraries and therefore you will not find a corresponding source file. To locate which component a source file belongs to, it is generally possible to search in the ESP-IDF source tree or look in the :ref:`linker-map-file` for the full path.
  191. Comparing Two Binaries
  192. ^^^^^^^^^^^^^^^^^^^^^^
  193. If making some changes that affect binary size, it is possible to use an ESP-IDF tool to break down the exact differences in size.
  194. This operation is not part of ``idf.py``, it is necessary to run the `esp_idf_size <https://github.com/espressif/esp-idf-size>`_ Python tool directly.
  195. To do so, first, locate the linker map file with the name ``PROJECTNAME.map`` in the build directory. The ``esp_idf_size`` tool performs its analysis based on the output of the linker map file.
  196. To compare with another binary, you also need its corresponding ``.map`` file saved from the build directory.
  197. For example, to compare two builds, one of which with the default :ref:`CONFIG_COMPILER_OPTIMIZATION` setting ``Debug (-Og)`` configuration while another with ``Optimize for size (-Os)``:
  198. .. code-block:: bash
  199. $ python -m esp_idf_size --diff build_Og/https_request.map build_Os/https_request.map
  200. <CURRENT> MAP file: build_Os/https_request.map
  201. <REFERENCE> MAP file: build_Og/https_request.map
  202. Difference is counted as <CURRENT> - <REFERENCE>, i.e. a positive number means that <CURRENT> is larger.
  203. Total sizes of <CURRENT>: <REFERENCE> Difference
  204. DRAM .data size: 14516 bytes 14956 -440
  205. DRAM .bss size: 15792 bytes 15808 -16
  206. Used static DRAM: 30308 bytes ( 150428 available, 16.8% used) 30764 -456 ( +456 available, +0 total)
  207. Used static IRAM: 78498 bytes ( 52574 available, 59.9% used) 83918 -5420 ( +5420 available, +0 total)
  208. Flash code: 509183 bytes 559943 -50760
  209. Flash rodata: 170592 bytes 176736 -6144
  210. Total image size:~ 772789 bytes (.bin may be padded larger) 835553 -62764
  211. We can see from the ``Difference`` column that changing this one setting caused the whole binary to be over 60 KB smaller and over 5 KB more RAM is available.
  212. It is also possible to use the ``diff`` mode to output a table of component-level (static library archive) differences:
  213. .. note::
  214. To get the output in JSON or CSV format using ``esp_idf_size``, it is possible to use the ``--format`` option.
  215. .. code-block:: bash
  216. python -m esp_idf_size --archives --diff build_Og/https_request.map build_Oshttps_request.map
  217. Also at the individual source file level:
  218. .. code-block:: bash
  219. python -m esp_idf_size --files --diff build_Og/https_request.map build_Oshttps_request.map
  220. Other options, like writing the output to a file, are available, pass ``--help`` to see the full list.
  221. .. _idf-size-linker-failed:
  222. Showing Size When Linker Fails
  223. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  224. If too much static memory is allocated, the linker will fail with an error such as ``DRAM segment data does not fit``, ``region `iram0_0_seg' overflowed by 44 bytes``, or similar.
  225. In these cases, ``idf.py size`` will not succeed either. However, it is possible to run ``esp_idf_size`` manually to view the **partial static memory usage**. The memory usage will miss the variables that could not be linked, so there still appears to be some free space.
  226. The map file argument is ``<projectname>.map`` in the build directory.
  227. .. code-block:: bash
  228. python -m esp_idf_size build/project_name.map
  229. It is also possible to view the equivalent of ``size-components`` or ``size-files`` output:
  230. .. code-block:: bash
  231. python -m esp_idf_size --archives build/project_name.map
  232. python -m esp_idf_size --files build/project_name.map
  233. .. _linker-map-file:
  234. Linker Map File
  235. ^^^^^^^^^^^^^^^
  236. .. note::
  237. This is an advanced analysis method, but it can be very useful. Feel free to skip ahead to :ref:`reducing-overall-size` and possibly come back to this later.
  238. The ``idf.py size`` analysis tools all work by parsing the GNU binutils ``linker map file``, which is a summary of everything the linker did when it created (i.e., linked) the final firmware binary file.
  239. Linker map files themselves are plain text files, so it is possible to read them and find out exactly what the linker did. However, they are also very complex and long, often exceeding 100,000 lines.
  240. The map file itself is broken into parts and each part has a heading. The parts are:
  241. - ``Archive member included to satisfy reference by file (symbol)``
  242. - This shows you: for each object file included in the link, what symbol (function or variable) was the linker searching for when it included that object file.
  243. - If you are wondering why some object file in particular was included in the binary, this part may give a clue. This part can be used in conjunction with the ``Cross Reference Table`` at the end of the file.
  244. .. note::
  245. Not every object file shown in this list ends up included in the final binary, some end up in the ``Discarded input sections`` list instead.
  246. - ``Allocating common symbols``
  247. - This is a list of some global variables along with their sizes. Common symbols have a particular meaning in ELF binary files, but ESP-IDF does not make much use of them.
  248. - ``Discarded input sections``
  249. - These sections were read by the linker as part of an object file to be linked into the final binary, but then nothing else referred to them, so they were discarded from the final binary.
  250. - For ESP-IDF, this list can be very long, as we compile each function and static variable to a unique section in order to minimize the final binary size. Specifically, ESP-IDF uses compiler options ``-ffunction-sections -fdata-sections`` and linker option ``--gc-sections``.
  251. - Items mentioned in this list **do not** contribute to the final binary.
  252. - ``Memory Configuration``, ``Linker script and memory map``
  253. - These two parts go together. Some of the output comes directly from the linker command line and the Linker Script, both provided by :doc:`/api-guides/build-system`. The linker script is partially generated from the ESP-IDF project using the :doc:`/api-guides/linker-script-generation` feature.
  254. - As the output of the ``Linker script and memory map`` part of the map unfolds, you can see each symbol (function or static variable) linked into the final binary along with its address (as a 16 digit hex number), its length (also in hex), and the library and object file it was linked from (which can be used to determine the component and the source file).
  255. - Following all of the output sections that take up space in the final ``.bin`` file, the ``memory map`` also includes some sections in the ELF file that are only used for debugging, e.g., ELF sections ``.debug_*``, etc. These do not contribute to the final binary size. You can notice the address of these symbols is a very small number, starting from ``0x0000000000000000`` and counting up.
  256. - ``Cross Reference Table``
  257. - This table shows the symbol (function or static variable) that the list of object file(s) refers to. If you are wondering why a particular thing is included in the binary, this will help determine what included it.
  258. .. note::
  259. Unfortunately, the ``Cross Reference Table`` does not only include symbols that made it into the final binary. It also includes symbols in discarded sections. Therefore, just because something is shown here does not mean that it was included in the final binary - this needs to be checked separately.
  260. .. note::
  261. Linker map files are generated by the GNU binutils linker ``ld``, not ESP-IDF. You can find additional information online about the linker map file format. This quick summary is written from the perspective of ESP-IDF build system in particular.
  262. .. _reducing-overall-size:
  263. Reducing Overall Size
  264. ---------------------
  265. The following configuration options reduces the final binary size of almost any ESP-IDF project:
  266. .. list::
  267. - Set :ref:`CONFIG_COMPILER_OPTIMIZATION` to ``Optimize for size (-Os)``. In some cases, ``Optimize for performance (-O2)`` will also reduce the binary size compared to the default. Note that if your code contains C or C++ Undefined Behavior then increasing the compiler optimization level may expose bugs that otherwise do not happen.
  268. - Reduce the compiled-in log output by lowering the app :ref:`CONFIG_LOG_DEFAULT_LEVEL`. If the :ref:`CONFIG_LOG_MAXIMUM_LEVEL` is changed from the default then this setting controls the binary size instead. Reducing compiled-in logging reduces the number of strings in the binary, and also the code size of the calls to logging functions.
  269. - Set the :ref:`CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL` to ``Silent``. This avoids compiling in a dedicated assertion string and source file name for each assert that may fail. It is still possible to find the failed assert in the code by looking at the memory address where the assertion failed.
  270. - Besides the :ref:`CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL`, you can disable or silent the assertion for the HAL component separately by setting :ref:`CONFIG_HAL_DEFAULT_ASSERTION_LEVEL`. It is to notice that ESP-IDF lowers the HAL assertion level in bootloader to be silent even if :ref:`CONFIG_HAL_DEFAULT_ASSERTION_LEVEL` is set to full-assertion level. This is to reduce the bootloader size.
  271. - Setting :ref:`CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT` removes specific error messages for particular internal ESP-IDF error check macros. This may make it harder to debug some error conditions by reading the log output.
  272. :esp32: - If the binary needs to run on only certain revision(s) of ESP32, increasing :ref:`CONFIG_ESP32_REV_MIN` to match can result in a reduced binary size. This will make a large difference if setting ESP32 minimum revision 3, and PSRAM is enabled.
  273. :esp32c3: - If the binary needs to run on only certain revision(s) of ESP32-C3, increasing :ref:`CONFIG_ESP32C3_REV_MIN` to match can result in a reduced binary size. This is particularly true if setting ESP32-C3 minimum revision 3 and using Wi-Fi, as some functionality was moved to ROM code.
  274. - Do not enable :ref:`CONFIG_COMPILER_CXX_EXCEPTIONS`, :ref:`CONFIG_COMPILER_CXX_RTTI`, or set the :ref:`CONFIG_COMPILER_STACK_CHECK_MODE` to Overall. All of these options are already disabled by default, but they have a large impact on binary size.
  275. - Disabling :ref:`CONFIG_ESP_ERR_TO_NAME_LOOKUP` removes the lookup table to translate user-friendly names for error values (see :doc:`/api-guides/error-handling`) in error logs, etc. This saves some binary size, but error values will be printed as integers only.
  276. - Setting :ref:`CONFIG_ESP_SYSTEM_PANIC` to ``Silent reboot`` saves a small amount of binary size, however this is **only** recommended if no one will use UART output to debug the device.
  277. :CONFIG_IDF_TARGET_ARCH_RISCV: - Seting :ref:`CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS` reduces binary size by replacing inlined prologues/epilogues with library calls.
  278. - If the application binary uses only one of the security versions of the protocomm component, then the support for others can be disabled to save some code size. The support can be disabled through :ref:`CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_0`, :ref:`CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_1` or :ref:`CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_2` respectively.
  279. .. note::
  280. In addition to the many configuration items shown here, there are a number of configuration options where changing the option from the default increases binary size. These are not noted here. Where the increase is significant is usually noted in the configuration item help text.
  281. .. _size-targeted-optimizations:
  282. Targeted Optimizations
  283. ^^^^^^^^^^^^^^^^^^^^^^
  284. The following binary size optimizations apply to a particular component or a function:
  285. .. only:: SOC_WIFI_SUPPORTED
  286. Wi-Fi
  287. @@@@@
  288. - Disabling :ref:`CONFIG_ESP_WIFI_ENABLE_WPA3_SAE` will save some Wi-Fi binary size if WPA3 support is not needed. Note that WPA3 is mandatory for new Wi-Fi device certifications.
  289. - Disabling :ref:`CONFIG_ESP_WIFI_SOFTAP_SUPPORT` will save some Wi-Fi binary size if soft-AP support is not needed.
  290. - Disabling :ref:`CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT` will save some Wi-Fi binary size if enterprise support is not needed.
  291. .. only:: esp32
  292. ADC
  293. @@@
  294. - Disabling ADC calibration features :ref:`CONFIG_ADC_CAL_EFUSE_TP_ENABLE`, :ref:`CONFIG_ADC_CAL_EFUSE_VREF_ENABLE`, :ref:`CONFIG_ADC_CAL_LUT_ENABLE` will save a small amount of binary size if ADC driver is used, at expense of accuracy.
  295. .. only:: SOC_BT_SUPPORTED
  296. Bluetooth NimBLE
  297. @@@@@@@@@@@@@@@@
  298. If using :doc:`/api-reference/bluetooth/nimble/index` then the following modifications can reduce binary size:
  299. .. list::
  300. :esp32: - Set :ref:`CONFIG_BTDM_CTRL_BLE_MAX_CONN` to 1 if only one Bluetooth LE connection is needed.
  301. - Set :ref:`CONFIG_BT_NIMBLE_MAX_CONNECTIONS` to 1 if only one Bluetooth LE connection is needed.
  302. - Disable either :ref:`CONFIG_BT_NIMBLE_ROLE_CENTRAL` or :ref:`CONFIG_BT_NIMBLE_ROLE_OBSERVER` if these roles are not needed.
  303. - Reducing :ref:`CONFIG_BT_NIMBLE_LOG_LEVEL` can reduce binary size. Note that if the overall log level has been reduced as described above in :ref:`reducing-overall-size` then this also reduces the NimBLE log level.
  304. lwIP IPv6
  305. @@@@@@@@@
  306. - Setting :ref:`CONFIG_LWIP_IPV6` to ``false`` will reduce the size of the lwIP TCP/IP stack, at the cost of only supporting IPv4.
  307. .. note::
  308. IPv6 is required by some components such as ``coap`` and :doc:`/api-reference/protocols/asio`. These components will not be available if IPV6 is disabled.
  309. lwIP IPv4
  310. @@@@@@@@@
  311. - If IPv4 connectivity is not required, setting :ref:`CONFIG_LWIP_IPV4` to ``false`` will reduce the size of the lwIP, supporting IPv6-only TCP/IP stack.
  312. .. note::
  313. Before disabling IPv4 support, please note that IPv6 only network environments are not ubiquitous and must be supported in the local network, e.g., by your internet service provider or using constrained local network settings.
  314. .. _newlib-nano-formatting:
  315. Newlib Nano Formatting
  316. @@@@@@@@@@@@@@@@@@@@@@
  317. By default, ESP-IDF uses Newlib "full" formatting for I/O functions (``printf()``, ``scanf()``, etc.)
  318. .. only:: CONFIG_ESP_ROM_HAS_NEWLIB_NANO_FORMAT
  319. Enabling the config option :ref:`CONFIG_NEWLIB_NANO_FORMAT` will switch Newlib to the "Nano" formatting mode. This is smaller in code size, and a large part of the implementation is compiled into the {IDF_TARGET_NAME} ROM, so it does not need to be included in the binary at all.
  320. The exact difference in binary size depends on which features the firmware uses, but 25 KB ~ 50 KB is typical.
  321. .. only:: CONFIG_ESP_ROM_HAS_NEWLIB_NORMAL_FORMAT
  322. Disabling the config option :ref:`CONFIG_NEWLIB_NANO_FORMAT` will switch Newlib to the "full" formatting mode. This will reduce the binary size, as {IDF_TARGET_NAME} has the full formatting version of the functions in ROM, so it does not need to be included in the binary at all.
  323. Enabling "Nano" formatting reduces the stack usage of each function that calls ``printf()`` or another string formatting function, see :ref:`optimize-stack-sizes`.
  324. "Nano" formatting does not support 64-bit integers, or C99 formatting features. For a full list of restrictions, search for ``--enable-newlib-nano-formatted-io`` in the `Newlib README file`_.
  325. .. only:: esp32c2
  326. .. note::
  327. :ref:`CONFIG_NEWLIB_NANO_FORMAT` is enabled by default on {IDF_TARGET_NAME}.
  328. .. _Newlib README file: https://sourceware.org/newlib/README
  329. .. _minimizing_binary_mbedtls:
  330. MbedTLS Features
  331. @@@@@@@@@@@@@@@@
  332. Under **Component Config** > **mbedTLS**, there are multiple mbedTLS features enabled default, some of which can be disabled if not needed to save code size.
  333. These include:
  334. - :ref:`CONFIG_MBEDTLS_HAVE_TIME`
  335. - :ref:`CONFIG_MBEDTLS_ECDSA_DETERMINISTIC`
  336. - :ref:`CONFIG_MBEDTLS_SHA512_C`
  337. - :ref:`CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS`
  338. - :ref:`CONFIG_MBEDTLS_SERVER_SSL_SESSION_TICKETS`
  339. - :ref:`CONFIG_MBEDTLS_SSL_CONTEXT_SERIALIZATION`
  340. - :ref:`CONFIG_MBEDTLS_SSL_ALPN`
  341. - :ref:`CONFIG_MBEDTLS_SSL_RENEGOTIATION`
  342. - :ref:`CONFIG_MBEDTLS_CCM_C`
  343. - :ref:`CONFIG_MBEDTLS_GCM_C`
  344. - :ref:`CONFIG_MBEDTLS_ECP_C` (Alternatively: Leave this option enabled but disable some of the elliptic curves listed in the sub-menu.)
  345. - :ref:`CONFIG_MBEDTLS_ECP_NIST_OPTIM`
  346. - :ref:`CONFIG_MBEDTLS_ECP_FIXED_POINT_OPTIM`
  347. - Change :ref:`CONFIG_MBEDTLS_TLS_MODE` if both server & client functionalities are not needed
  348. - Consider disabling some cipher suites listed in the ``TLS Key Exchange Methods`` sub-menu (i.e., :ref:`CONFIG_MBEDTLS_KEY_EXCHANGE_RSA`)
  349. The help text for each option has some more information for reference.
  350. .. important::
  351. It is **strongly not recommended to disable all these mbedTLS options**. Only disable options of which you understand the functionality and are certain that it is not needed in the application. In particular:
  352. - Ensure that any TLS server(s) the device connects to can still be used. If the server is controlled by a third party or a cloud service, it is recommended to ensure that the firmware supports at least two of the supported cipher suites in case one is disabled in a future update.
  353. - Ensure that any TLS client(s) that connect to the device can still connect with supported/recommended cipher suites. Note that future versions of client operating systems may remove support for some features, so it is recommended to enable multiple supported cipher suites, or algorithms for redundancy.
  354. If depending on third party clients or servers, always pay attention to announcements about future changes to supported TLS features. If not, the {IDF_TARGET_NAME} device may become inaccessible if support changes.
  355. .. only:: CONFIG_ESP_ROM_HAS_MBEDTLS_CRYPTO_LIB
  356. Enabling the config option :ref:`CONFIG_MBEDTLS_USE_CRYPTO_ROM_IMPL` will use the crypto algorithms from mbedTLS library inside the chip ROM.
  357. Disabling the config option :ref:`CONFIG_MBEDTLS_USE_CRYPTO_ROM_IMPL` will use the crypto algorithms from the ESP-IDF mbedtls component library. This will increase the binary size (flash footprint).
  358. .. note::
  359. Not every combination of mbedTLS compile-time config is tested in ESP-IDF. If you find a combination that fails to compile or function as expected, please report the details on `GitHub <https://github.com/espressif/esp-idf>`_.
  360. VFS
  361. @@@
  362. :doc:`/api-reference/storage/vfs` feature in ESP-IDF allows multiple filesystem drivers and file-like peripheral drivers to be accessed using standard I/O functions (``open``, ``read``, ``write``, etc.) and C library functions (``fopen``, ``fread``, ``fwrite``, etc.). When filesystem or file-like peripheral driver functionality is not used in the application, this feature can be fully or partially disabled. VFS component provides the following configuration options:
  363. * :ref:`CONFIG_VFS_SUPPORT_TERMIOS` — can be disabled if the application does not use ``termios`` family of functions. Currently, these functions are implemented only for UART VFS driver. Most applications can disable this option. Disabling this option reduces the code size by about 1.8 KB.
  364. * :ref:`CONFIG_VFS_SUPPORT_SELECT` — can be disabled if the application does not use the ``select`` function with file descriptors. Currently, only the UART and eventfd VFS drivers implement ``select`` support. Note that when this option is disabled, ``select`` can still be used for socket file descriptors. Disabling this option reduces the code size by about 2.7 KB.
  365. * :ref:`CONFIG_VFS_SUPPORT_DIR` — can be disabled if the application does not use directory-related functions, such as ``readdir`` (see the description of this option for the complete list). Applications that only open, read and write specific files and do not need to enumerate or create directories can disable this option, reducing the code size by 0.5 KB or more, depending on the filesystem drivers in use.
  366. * :ref:`CONFIG_VFS_SUPPORT_IO` — can be disabled if the application does not use filesystems or file-like peripheral drivers. This disables all VFS functionality, including the three options mentioned above. When this option is disabled, :doc:`/api-reference/system/console` can not be used. Note that the application can still use standard I/O functions with socket file descriptors when this option is disabled. Compared to the default configuration, disabling this option reduces code size by about 9.4 KB.
  367. .. only:: CONFIG_ESP_ROM_HAS_HAL_SYSTIMER or CONFIG_ESP_ROM_HAS_HAL_WDT
  368. HAL
  369. @@@
  370. .. list::
  371. :CONFIG_ESP_ROM_HAS_HAL_SYSTIMER: * Enabling :ref:`CONFIG_HAL_SYSTIMER_USE_ROM_IMPL` can reduce the IRAM usage and binary size by linking in the systimer HAL driver of ROM implementation.
  372. :CONFIG_ESP_ROM_HAS_HAL_WDT: * Enabling :ref:`CONFIG_HAL_WDT_USE_ROM_IMPL` can reduce the IRAM usage and binary size by linking in the watchdog HAL driver of ROM implementation.
  373. Heap
  374. @@@@
  375. .. list::
  376. * Enabling :ref:`CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASH` can reduce the IRAM usage and binary size by placing the entirety of the heap functionalities in flash memory.
  377. :CONFIG_ESP_ROM_HAS_HEAP_TLSF: * Enabling :ref:`CONFIG_HEAP_TLSF_USE_ROM_IMPL` can reduce the IRAM usage and binary size by linking in the TLSF library of ROM implementation.
  378. Bootloader Size
  379. ---------------
  380. This document deals with the size of an ESP-IDF app binary only, and not the ESP-IDF :ref:`second-stage-bootloader`.
  381. For a discussion of ESP-IDF bootloader binary size, see :ref:`bootloader-size`.
  382. IRAM Binary Size
  383. ----------------
  384. If the IRAM section of a binary is too large, this issue can be resolved by reducing IRAM memory usage. See :ref:`optimize-iram-usage`.