Native memory refers to the heap memory allocated using malloc, new, realloc, or calloc function in the code and the memory address space mapped through mmap. Native memory occupies a large proportion of the process memory and is prone to memory leakage. When analyzing native memory distribution and usage, you need some tools, tests, and analysis skills. The Allocation template of DevEco Studio Profiler can be used to record functions such as malloc and free in the basic library and capture native memory allocation and release records, including the size and stack data, to analyze the native memory usage.
Stack capturing of DevEco heap memory allocation and release
The Allocation template of DevEco Studio Profiler helps users analyze the information about heap memory allocation and release, memory mapping, and call stack, including the released and unreleased memory. The procedure is as follows:

Stack capturing properties when the Allocation template of DevEco Studio Profiler is used
This section describes the properties configured in step 5 of the previous section.

When analyzing the data in the Allocation template of DevEco Studio Profiler, you can select All Heap to analyze the heap memory data. All Anonymous VM column displays the allocated anonymous memory, which is usually the memory allocated and released through mmap and munmap functions. The memory allocation is automatically performed by the process, and developers can hardly control it.
Memory allocation statistics
After selecting Statistics, you can view the size of native memory allocated to the process during stack capturing. You can view the total memory allocated to the process in the Total column.
You can view the total memory that is allocated but not released by the process in the Persistent column. In addition, you can view the size clustering information of memory allocation and sort the information as required.

Call stack information
After selecting Call Trees, you can expand the stack to be analyzed. The stack is in the sequence of call chains from top to bottom. The bottom of the stack is usually the operator new function. The green part in the following figure is the ArkTS stack, which can be captured after Record JS Stack is enabled. You can choose to view the released or unreleased memory as required and search for the symbols to be filtered.

Statistics mode information
When Statistics Mode is selected, the captured information is simplified. The specific time of each call stack is not displayed. Instead, the information is displayed periodically in cluster mode. Therefore, the native memory allocation changes in a period are not displayed. You can view the total number of stack occurrences and the total size in the Count column, as shown in the following figure.

After capturing a trace using the Allocation template, you can analyze the trace as follows:

When the ArkTS/JS code logic on the application triggers an NAPI call and the native layer is allocated with memory, the ArkTS/JS stack is transmitted to the Profiler and native stack for stitching, showing a complete call chain. Therefore, the Profiler can be used to analyze not only memory leakage issues, but also service logic and calling relationships.



Question: Why is it not unavailable to capture data of applications not running the debug version?
Answer: Due to security restrictions, the Profiler does not support capturing data of applications not running the debug version.
Question: Why is the heap memory size captured by the Allocation template smaller than the native heap value in the hidumper command output?
Answer: The Allocation template collects stacks of the user mode memory allocated using malloc, mmap, and other heap memory allocation functions in the target process. The memory that is not released in time and thread cache are not counted, but are included in the native heap of smaps.
Question: Why is the data captured by selecting FP in the Allocation template abnormal on the devices not using the AArch64 architecture?
Answer: After FP is selected in the Allocation template, the tuning of devices not using the AArch64 architecture is not supported.