Chapter 2. API Reference
/home/user/projects/esp/esp-idf/components/freertos/port.c:355 (discriminator 1)
[0.302436000] HEAP: Allocated 4 bytes @ 0x3ffafff0 from task "alloc" on core 0 by:
/home/user/projects/esp/esp-idf/examples/system/sysview_tracing_heap_log/main/
sysview_heap_log.c:47
→
/home/user/projects/esp/esp-idf/components/freertos/port.c:355 (discriminator 1)
Found 10 leaked bytes in 4 blocks.
Heap Tracing To Find Heap Corruption
When a region in heap is corrupted, it may be from some other part of the program which allocated memory at a
nearby address.
If you have some idea at what time the corruption occurred, enabling heap tracing in HEAP_TRACE_ALL mode
allows you to record all of the functions which allocated memory, and the addresses of the allocations.
Using heap tracing in this way is very similar to memory leak detection as described above. For memory which is
allocated and not freed, the output is the same. However, records will also be shown for memory which has been
freed.
Performance Impact
Enabling heap tracing in menuconfig increases the code size of your program, and has a very
small negative impact on performance of heap allocation/free operations even when heap tracing is not running.
When heap tracing is running, heap allocation/free operations are substantially slower than when heap tracing is
stopped. Increasing the depth of stack frames recorded for each allocation (see above) will also increase this perfor-
mance impact.
False-Positive Memory Leaks
Among things which may show up here, but are not memory leaks:
• Any
memory
which
heap_trace_stop()
• Allocations may be made by other tasks in the system. Depending on the timing of these tasks, it's quite
possible this memory is freed after
• The first time a task uses stdio - for example, when it calls printf() - a lock (RTOS mutex semaphore) is
allocated by the libc. This allocation lasts until the task is deleted.
• Certain uses of printf(), such as printing floating point numbers, will allocate some memory from the heap
on demand. These allocations last until the task is deleted.
• The Bluetooth, WiFi, and TCP/IP libraries will allocate heap memory buffers to handle incoming or outgoing
data. These memory buffers are usually short lived, but some may be shown in the heap leak trace if the data
was received/transmitted by the lower levels of the network while the leak trace was running.
• TCP connections will continue to use some memory after they are closed, because of the TIME_WAIT state.
After the TIME_WAIT period has completed, this memory will be freed.
One way to differentiate between "real"and "false positive"memory leaks is to call the suspect code multiple
times while tracing is running, and look for patterns (multiple matching allocations) in the heap trace output.
API Reference - Heap Tracing
Header File
•
components/heap/include/esp_heap_trace.h
Functions
Espressif Systems
Heap tracing can also be used to help track down heap corruption.
Not everything printed by
is
allocated
after
heap_trace_start()
will appear in the leak dump.
heap_trace_stop()
991
Submit Document Feedback
heap_trace_dump()
is called.
(continued from previous page)
is necessarily a memory leak.
but
then
freed
after
Release v4.4
Need help?
Do you have a question about the ESP32-S2 and is the answer not in the manual?
Questions and answers