Memory tracing
Mbed OS provides a set of functions that you can use to study the runtime memory allocation pattern of your software: which sections of the code allocate and free memory and how much memory they need.
You must enable the memory-tracing-enabled
setting in the Mbed OS platform configuration options to enable memory tracing. We recommend doing this by adding it to your mbed_app.json
:
{
"target_overrides": {
"*": {
"platform.memory-tracing-enabled": true
}
}
}
You can use the mbed_mem_trace_set_callback
API to set the callback for memory tracing. The callback is invoked every time you call standard allocation functions, such as malloc
, realloc
, calloc
and free
.
For a step-by-step guide about how to use optimize memory using runtime memory tracing, please see our runtime memory tracing tutorial.
Memory tracing functions reference
Memory tracing example
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include "mbed.h"
#include "mbed_mem_trace.h"
int main()
{
mbed_mem_trace_set_callback(mbed_mem_trace_default_callback);
while (true) {
void *p = malloc(50);
printf("50B allocated at %p\n", p);
ThisThread::sleep_for(500);
free(p);
printf("50B freed at %p\n\n", p);
}
}