目录

Fast Locating Native Memory Leaks in the Development State

Introduction

Native memory leaks refer to the leakage of memory allocated at the C/C++ layer (through the NDK or system bottom layer) due to improper release. This document describes high-frequency memory leak scenarios and native memory leak analysis cases to help you quickly locate native memory leak issues in your apps.

Common Leak Scenarios

Leakage caused by native exceptions

Native memory leak caused by ArkTS references

Criterion-based Troubleshooting Process

Main method for checking native memory leaks: Use Native Heap for analysis. Native Heap is suitable for detecting complex leakage patterns (such as improper lifecycle management and excessive caching). It requires analysis based on the call stack and service scenarios.

  1. Reproduction and log obtaining: Use the Allocation template of DevEco Profiler to enable the statistics mode and record the memory leak scenario. Repeat the suspected memory leak scenario for multiple times to reproduce the problem.
  2. Identifying the leak point: Click the Native Heap lane. On the Call Tree tab page in the lower part, select Created & Existing to view the call stacks with high memory usage.
  3. Analyzing the call stack: Search for the symbol names that have high memory usage and are closely related to the service code in the call stack information. These symbol names are highlighted in the category. Analyze the related code based on the call stack (double-click to jump to the source code) and locate the cause of the memory not being released.
  4. Code review: Based on the call stack, review the memory holding logic in the related code and locate the root cause of the leak.
    • If the service logic is normal, the native memory leak is caused by ArkTS references. For details, see ArkTS Memory Leak Analysis Case.
    • If the service logic is abnormal: Modify the related code.
  5. Fixing and verification: After modifying the code, repeat steps 1 and 2 to ensure that the memory curve becomes stable.

The following figure shows the overall process of using Native Heap for analysis.

Native Memory Leak Analysis Case

[h2]Using Native Heap for Analysis

Case Background

Symptom: In this case, the problem scenario is reproduced through repeated operations. It is found that the app Native Heap memory usage keeps increasing in a stepwise manner.

Preliminary judgment: Use the Allocation profiling mode to record the memory usage increase process and observe the Native Heap curve in the memory lane. The curve shows a typical stepwise increase, indicating that native memory leakage occurs.

Analysis process

Step 1: Record leak scenarios using Allocation.

1. Use the Allocation template of DevEco Studio Profiler to analyze the information about heap memory allocation and release, memory mapping, and call stacks. The information includes deallocated memory and memory that has not been deallocated. The procedure is as follows: Start the app process, select the Profiler tool, select the device and app process, select the Allocation template, create a session, and configure recording options.

2. Enable the statistics mode and enable asynchronous stack recording (to facilitate tracing to the service code).

3. Click the button to start recording and reproduce the problem scenario.

Step 2: View the memory allocation stack.

  1. Select the Native Heap sub-lane in All Heap.
  2. On the Statistics tab page in the details area, select Created & Existing.
    • All Allocations: indicates all memory allocation information within the selected time range.
    • Created & Existing: indicates the memory data that is allocated after the start point of the selected range and is not released before the end point of the selected range. This option is selected by default.
    • Created & Released: indicates the memory that is allocated after the start of the selected range and released before the end of the selected range.

3. Switch to the Call Trees tab page. This tab page displays detailed information about the memory allocation stack.

Step 3: Analyze the memory allocation stack.

Search for the symbol names that have high memory usage and are closely related to the service code in the memory allocation stack information. These symbol names are highlighted in the category. Analyze the related code based on the call stack (double-click to jump to the source code) and locate the cause of the memory not being released. It can be seen that the service code malloc performs caching operations, but the free method deallocate memory is not added.

[h2]Optimization and Rectification

  1. Modify the code to add the free method deallocate memory.
  2. Run the app again and use Allocation to record the memory allocation stack.
  3. Repeat the memory leak scenario multiple times.
  4. Verification result:
    • The memory curve does not increase significantly.
    • The memory leak issue has been resolved.