Chapter 4. API Guides
IRAM Binary Size If the IRAM section of a binary is too large, this issue can be resolved by reducing IRAM
memory usage. See Optimizing IRAM Usage.
Minimizing RAM Usage
In some cases, a firmware application’s available RAM may run low or run out entirely. In these cases, it’s necessary
to tune the memory usage of the firmware application.
In general, firmware should aim to leave some“headroom”of free internal RAM in order to deal with extraordinary
situations or changes in RAM usage in future updates.
Background Before optimizing ESP-IDF RAM usage, it’s necessary to understand the basics of ESP32-S2 mem-
ory types, the difference between static and dynamic memory usage in C, and the way ESP-IDF uses stack and heap.
This information can all be found in Heap Memory Allocation.
Measuring Static Memory Usage The idf.py tool can be used to generate reports about the static memory usage
of an application. Refer to the Binary Size chapter for more information.
Measuring Dynamic Memory Usage ESP-IDF contains a range of heap APIs for measuring free heap at runtime.
See Heap Memory Debugging.
Note: In embedded systems, heap fragmentation can be a significant issue alongside total RAM usage. The heap
measurement APIs provide ways to measure the “largest free block”. Monitoring this value along with the total
number of free bytes can give a quick indication of whether heap fragmentation is becoming an issue.
Reducing Static Memory Usage
• Reducing the static memory usage of the application increases the amount of RAM available for heap at run-
time, and vice versa.
• Generally speaking, minimizing static memory usage requires monitoring the .data and .bss sizes. For tools to
do this, see Measuring Static Sizes.
• Internal ESP-IDF functions do not make heavy use of static RAM allocation in C. In many instances (including:
Wi-Fi library) “static”buffers are still allocated from heap, but the allocation is done once when the feature
is initialized and will be freed if the feature is deinitialized. This is done in order to maximize the amount of
free memory at different points in the application life-cycle.
To minimize static memory use:
• Declare structures, buffers, or other variables const whenever possible. Constant data can be stored in flash
not RAM. This may require changing functions in the firmware to take const * arguments instead of mutable
pointer arguments. These changes can also reduce the stack usage of some functions.
Reducing Stack Sizes In FreeRTOS, task stacks are usually allocated from the heap. The stack size for each task
is fixed (passed as an argument to xTaskCreate()). Each task can use up to its allocated stack size, but using
more than this will cause an otherwise valid program to crash with a stack overflow or heap corruption.
Therefore, determining the optimum sizes of each task stack can substantially reduce RAM usage.
To determine optimum task stack sizes:
• Combine tasks. The best task stack size is 0 bytes, achieved by combining a task with another existing task.
Anywhere that the firmware can be structured to perform multiple functions sequentially in a single task will
increase free memory. In some cases, using a“worker task”pattern where jobs are serialized into a FreeRTOS
queue (or similar) and then processed by generic worker tasks may help.
• Consolidate task functions. String formatting functions (like printf) are particularly heavy users of stack,
so any task which doesn’t ever call these can usually have its stack size reduced.
Espressif Systems 1452
Submit Document Feedback
Release v4.4