Contributed by 爱笑, organized by Ai-Thinker
How to Locate and Analyze Static and Dynamic Memory Usage in Code During Ai-WB2 Series Secondary Development
1. Software and Hardware Preparation Eclipse, bl_iot_sdk, an Ai-WB2 series module or development board, and the static memory analysis tool bouffalo_parse_tool-win32. Download link: <https://docs.ai-thinker.com/_media/bouffalo_parse_tool-win32.zip> Download the analysis tool
2. Building the Project
First open the Eclipse project and import bl_iot_sdk. This time we use the aws_iot_core project to read the static memory size used by the code. After selecting the corresponding project, click "Apply and Close" to close the window, then right-click the SDK and select "Build Project" to compile.

After the build completes, the following screen is displayed

3. Getting Static Memory Usage
After the build, open the SDK directory, find the build_out folder of the compiled project, and select the aws_iot_core.map file, as shown below

Open the Bouffalo Lab Parse Tool, select the "Memory Map (.a)" option, and drag the aws_iot_core.map file into it

Once the file is loaded, the tool reads the map file information. You can see that this project uses 52,376 bytes of static memory, which converts to about 51 KB, as shown below

4. Getting Dynamic Memory Usage
In the helloworld project, modify the main.c file. To make dynamic memory usage easier to observe, this demo measures the dynamic memory before and after the task is started. The specific code changes are as follows:
Click to expand full code
void helloworld(void *pvParameters)
{
for (;;)
{
log_step(ci_table_step_init);
log_step(ci_table_step_log);
log_step(ci_table_step_end);
vTaskDelay(1000 / portTICK_RATE_MS);
}
vTaskDelete(NULL);
}
void main(void)
{
int remain_ram = 0, remain_min_heap = 0;
remain_ram = xPortGetFreeHeapSize();
remain_min_heap = xPortGetMinimumEverFreeHeapSize();
printf("remain_ram1:%d\r\n remain_in_heap:%d\r\n", remain_ram, remain_min_heap);
xTaskCreate(helloworld, (char *)"main_entry", 1024, NULL, 15, NULL);
remain_ram = xPortGetFreeHeapSize();
remain_min_heap = xPortGetMinimumEverFreeHeapSize();
printf("remain_ram1:%d\r\n remain_in_heap:%d\r\n", remain_ram, remain_min_heap);
}After making the changes above, flash the firmware with the flashing tool and read the power-on log, as shown below:

(1) Memory remaining before the task is created (2) Memory remaining after the task is created 5. Contact Us That is all for this sharing session. The goal is to help everyone quickly adapt to developing with the new product solution. More resources are available on our official website. Official website: <https://www.ai-thinker.com> Development resources: <https://docs.ai-thinker.com/> Technical support: support@aithinker.com

