DEV Community

SameX
SameX

Posted on

HarmonyOS Next: Basics of Memory Management and Garbage Collection

This article aims to deeply explore the technical details of the Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices.
It mainly serves as a vehicle for technical sharing and communication. Mistakes and omissions are inevitable. Colleagues are welcome to put forward valuable opinions and questions so that we can make progress together.
This article is original content, and any form of reprint must indicate the source and the original author.

Introduction

HarmonyOS Next, as an operating system independently developed by Huawei, has attracted much attention for its lightweight and cross-platform features. Among them, the memory management and garbage collection (GC) mechanism, as the key technologies to ensure system performance, are of great importance to developers. This article will guide you to initially explore the memory management mechanism of HarmonyOS Next and deeply understand its garbage collection algorithms, helping developers better master memory management skills and improve application performance.

Memory Allocation in ArkTS

HarmonyOS Next is developed using the ArkTS programming language, and its memory allocation mechanism is different from that of traditional languages such as Java and C++. The memory in ArkTS is mainly divided into two types:

  • Stack: It is used to store simple types (such as numbers, strings, etc.) and local variables of function calls. The stack space is automatically allocated and released by the operating system, and developers do not need to manage it.
  • Heap: It is used to store the memory space of reference types (such as objects, arrays, etc.). The heap space needs to be managed by the ArkTS engine, and developers need to release the memory that is no longer used through the garbage collection mechanism. ### Garbage Collection (GC) Concept Garbage Collection (abbreviated as GC) is an automated memory management mechanism used to recycle the memory space that is no longer used in a program. The purpose of GC is to avoid memory leaks and improve memory utilization. Currently, the mainstream GC algorithms are mainly divided into two categories:
  • Reference Counting: It tracks the number of references of an object by maintaining a reference counter for each object. When the reference count is 0, it means that the object is no longer referenced by any variable and can be recycled.
  • Object Tracing (also known as Tracing GC): It finds all unreachable objects for recycling by traversing the object graph. The object graph is a structure composed of objects and their reference relationships, and the entire object graph can be traversed by algorithms such as depth-first search. The ArkTS runtime uses a combination of reference counting and object tracing algorithms based on the generational model (young generation/old generation) and executes GC tasks in parallel and concurrently, thus achieving high-performance memory recycling performance in different scenarios. ### GC Triggering Mechanisms The GC mechanism of HarmonyOS Next is mainly divided into three types:
  • Young GC: It mainly recycles the young generation space, which is used to store newly created objects. The triggering conditions include: insufficient young generation space, exceeding the preset threshold, etc.
  • Old GC: It mainly recycles the old generation space, which is used to store objects with a long survival time. The triggering conditions include: insufficient old generation space, exceeding the preset threshold, etc.
  • Full GC: It cleans up the entire heap space and recycles all the objects that are no longer used. The triggering conditions include: the application switches to the background, manual triggering, etc. ### An Example The following sample code shows how to create an object in ArkTS and trigger garbage collection:
// Create an object
let obj = { name: "Zhang San", age: 30 };
// Add the object to an array
let array = [obj, obj];
// Delete the object
array.splice(0, 1);
// Manually trigger garbage collection
ArkTools.hintGC();
Enter fullscreen mode Exit fullscreen mode

In the above code, we created an object and added it to an array. Then, we deleted the first element in the array. At this time, the reference count of the object obj becomes 0, so it can be regarded as a garbage object. Finally, we call the ArkTools.hintGC() method to manually trigger garbage collection to recycle the objects that are no longer used.

GC Log Analysis

HarmonyOS Next provides rich GC log information, which can help developers analyze GC behavior and performance. Here are some common GC log keywords:

  • [HPP YoungGC]: It indicates young GC, mainly recycling the young generation space.
  • [HPP OldGC]: It indicates old GC, mainly recycling the old generation space.
  • [CompressGC]: It indicates full GC, cleaning up the entire heap space.
  • IsInBackground: It indicates whether the application is running in the background.
  • SensitiveStatus: It indicates whether it is in a performance-sensitive scenario.
  • OnStartupEvent: It indicates whether it is in an application cold-start scenario. By analyzing the GC logs, developers can understand the reasons for GC triggering, the time consumption, the memory occupancy, etc., and thus optimize the memory management strategy to improve application performance. ### Summary The memory management and garbage collection mechanism of HarmonyOS Next are the key technologies to ensure system performance. By understanding the memory allocation mechanism of ArkTS, the GC algorithms, and the triggering conditions, developers can better master memory management skills and improve application performance. Meanwhile, by analyzing the GC logs, we can have a deeper understanding of GC behavior and conduct targeted optimizations.

Top comments (0)