DEV Community

Cover image for HarmonyOS Next ArkTS Memory Leak Analysis
kouwei qing
kouwei qing

Posted on

HarmonyOS Next ArkTS Memory Leak Analysis

HarmonyOS Next ArkTS Memory Leak Analysis

Memory leaks are a common issue that can lead to performance degradation, application crashes, and even device memory exhaustion. In Android development, common causes of memory leaks include:

  1. View Holders Retaining Activity/Fragment References: When inner classes or anonymous classes within an Activity or Fragment hold references to the parent component, preventing garbage collection.
  2. Improper Context Handling: Directly using Context in Activities or Fragments without proper management can lead to leaks.
  3. Strong References in Listeners/Callbacks: Listeners or callbacks holding strong references to external objects, which persist even after the Activity/Fragment is destroyed.
  4. Unclosed Resources: Database connections, file streams, or network connections left open.
  5. Improper Image Loading/Release: Large images or incorrect handling of image resources.
  6. Collection Classes Holding References: ArrayList, HashMap, etc., retaining strong references to external objects.
  7. Singletons Holding Resources: Singletons retaining other resources without proper cleanup.

In Android, tools like Android Studio's Memory Profiler help detect and locate memory leaks. But what about HarmonyOS Next? What are the common memory leak scenarios, and how can we quickly identify them? This article explores ArkTS memory leak analysis.


Memory Leak Analysis Steps in HarmonyOS Next

  1. Capture a Snapshot Before the Leak: Take a memory snapshot before the suspected leak occurs.
  2. Trigger the Leak Operation: Perform the operation suspected to cause the leak.
  3. Capture a Second Snapshot: Take another snapshot after the leak operation.
  4. Compare Snapshots: Analyze differences between the two snapshots to identify leaked objects.
  5. Iterative Comparison: If multiple objects appear in both snapshots, repeat the leak-triggering process and compare again to observe linear growth trends, narrowing down the leak source.

Recording Snapshot Template Data

Use DevEco Studio's Profiler tool to record memory snapshots:

  1. Open Profiler, select the target device, and choose the debugging process.
  2. Navigate to Snapshot, select it, and click "Create Session". Create Session
  3. Click the green Play button to start recording. Start Recording
  4. The recording indicator in the timeline confirms active recording. Recording Active
  5. Click the camera icon to take a snapshot; a purple bar indicates completion. Capture Snapshot
  6. Export the snapshot data after recording. Export Snapshot

Analyzing Snapshot Data

Import the snapshot for analysis or directly inspect the purple snapshot regions:

Analyze Snapshot

Focus on analyzing ArkTS objects and methods.

Object Analysis

JSArray

Expanding a JSArray reveals its elements:

JSArray Structure

  • _proto_: Prototype object (consistent across all arrays).
  • length: Built-in property accessor for array length.
JSObject

Expanding a JSObject shows its internal properties:

JSObject Structure

Example Code:

class People {
  old: number;
  name: string;
  constructor(old: number, name: string) {
    this.old = old;
    this.name = name;
  }
  printOld() { console.log("old = ", this.old); }
  printName() { console.log("name = ", this.name); }
}
let p = new People(20, "Tom");
Enter fullscreen mode Exit fullscreen mode

Snapshot Data:

JSObject Snapshot

  • Object 92729 corresponds to the People class, showing its properties and methods.
  • The _proto_ of an instance points to the class declaration, which includes the constructor.
  • Multiple instances share the same class declaration and constructor.
JSFunction

All JSFunction instances are listed under the (closure) tag:

JSFunction List

Expanding a function reveals its properties:

JSFunction Details

Key Properties:

  • HomeObject: Parent object to which the function belongs.
  • _proto_: Prototype object.
  • LexicalEnv: Closure context.
  • name: Built-in accessor for the function name.
  • FunctionExtraInfo: Additional metadata (e.g., NAPI interface addresses).
  • ProtoOrHClass: Prototype or hidden class.

Anonymous/Unnamed Functions:

  • If a function is unnamed (anonymous()), it may be a dynamically created function.
  • If labeled JSFunction(), it might be a framework-generated function without an explicit name.
  • Use references to infer the function's purpose: Function References

Method Analysis

Identifying Object Names:

  • For declared objects, check the constructor property. Constructor Check
  • For instances, expand _proto_ to find the constructor. Instance Constructor
  • Search code for distinctive properties to match objects.

Inheritance Analysis:

Expand _proto_ to trace inheritance hierarchies:

Inheritance Example

The above shows that Man inherits from People.


Summary

This guide outlines the process of identifying and analyzing memory leaks in HarmonyOS Next ArkTS applications. By leveraging DevEco Studio's Profiler and snapshot comparison, developers can pinpoint leaked objects, inspect their properties, and trace their origins. Key steps include:

  1. Capturing snapshots before and after suspected leaks.
  2. Comparing data to identify abnormal object retention.
  3. Analyzing objects (e.g., JSArray, JSObject, JSFunction) to understand their lifecycle.

With these tools, developers can efficiently diagnose and resolve memory issues, ensuring robust and performant HarmonyOS applications.

Top comments (0)