Tuning Process
Networking Environment
Prerequisites
Tuning Strategy
- Use the DevKit System Diagnosis tool to analyze memory usage.
- View the analysis report and locate memory leaks based on the call stack.
- After the program is optimized, use the tool again to check whether the problem has been resolved.
Tuning Process
- Create a memory usage diagnosis task.
Select the application to be diagnosed, enter the server password, set the sampling duration, and click Verify and Create to create a task.

- Wait until the task is complete and view the report.
View the stack information and memory usage chart to monitor memory consumption. You will notice that the memory usage of a module increases over time, indicating a memory leak during this period.

- View the call stack information.
The memory leak point is found at line 9 in the mem_leak.c file.

- View the source file.
It is found that memory allocation occurs at line 9 of the mem_leak.c file. Upon checking the code, you can see that the allocated memory is not released.

- Modify the source code to release the occupied memory.
The new source file is mem_leak_fix.c.

- Use the tool to perform diagnosis again.
Replace the current application path with the path to the optimized application (mem_leak_fix) and create a diagnosis task again.

According to the report, memory usage no longer increases continuously, indicating that the problem has been resolved. (The two memory allocations shown in the report are internal TLS memory from the call to pthread_create and buffer memory from the call to printf.)
Summary
The initial test program dynamically allocates memory for data caching. However, it does not call the free function to release the allocated memory before it stops running. As a result, the memory is not reclaimed. Although the program logic is simple and the program exits finally, a typical memory leak occurs. This tuning practice demonstrates a typical process of locating and rectifying memory leaks. With the DevKit System Diagnosis tool, you can quickly locate the root cause of memory leaks and verify the tuning effect.