DEV Community

SameX
SameX

Posted on

The Garbage Collection Model in the ArkTS Programming Language: A Detailed Explanation of Generational GC

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

Garbage collection (GC) is an important memory management mechanism in modern programming languages, and its efficiency directly affects the performance of applications. As the development language of the HarmonyOS system, ArkTS adopts a generational garbage collection model at runtime, dividing the memory space into the young generation and the old generation, and using different recycling algorithms for optimization. This article will conduct an in-depth analysis of the generational GC model of ArkTS to help developers better understand its principles and optimization strategies.

The GC Generational Model

The generational GC model of ArkTS divides the memory space into the following parts:

  • Young Generation: Used to store newly created objects with a relatively low survival rate. The young generation space is divided into two semi-spaces (SemiSpace), which are respectively used for object creation and recycling.
  • Old Generation: Used to store objects with a relatively long survival time and a relatively high survival rate. The old generation space is used to store the objects that were not recycled in the young generation GC.
  • HugeObjectSpace: Used to store large objects, such as large arrays. The huge object space is managed in a separate area.
  • ReadOnlySpace: Used to store read-only data during runtime, such as string constants.
  • NonMovableSpace: Used to store immovable objects, such as system class objects.
  • SnapshotSpace: Used for the space when dumping heap snapshots.
  • MachineCodeSpace: Used to store the machine code of the program. ### The Trigger Thresholds for Each Generation of GC The GC triggering mechanism of ArkTS is based on the occupancy of the memory space, and the triggering conditions for different generations of GC are different:
  • Young Generation GC: When the young generation space is insufficient or reaches the preset threshold, the young generation GC will be triggered. The young generation GC mainly uses the Copying algorithm to copy the surviving objects to another semi-space and recycle the old semi-space.
  • Old Generation GC: When the old generation space is insufficient or reaches the preset threshold, the old generation GC will be triggered. The old generation GC mainly uses algorithms such as Sweep and Compact to clean and compress the old generation space. ### The Generational Recycling Algorithms The generational GC model of ArkTS adopts multiple recycling algorithms to adapt to the memory characteristics of different generations:
  • Copying Algorithm: The young generation space is divided into two semi-spaces (SemiSpace). Only one semi-space is used for object allocation each time. When the space of this semi-space is insufficient, the young generation GC will be triggered. The surviving objects will be copied to another semi-space, and the old semi-space will be recycled.
  • Sweep Algorithm: Traverse the old generation space to recycle the objects that are no longer used.
  • Compact Algorithm: Move the objects in the old generation space together to improve the memory utilization rate. ### Optimization of the GC Process The GC process of ArkTS adopts concurrent and parallel optimization strategies to improve the efficiency of garbage collection:
  • Concurrent Mark: During the running of the application program, the object graph is traversed concurrently for marking, reducing the suspension time of the main thread.
  • Parallel Collection: Use multiple threads to execute garbage collection tasks in parallel to improve the recycling efficiency. ### An Example The following sample code shows how to create objects in ArkTS and trigger the young generation and old generation GCs:
// Create objects
let obj1 = { name: "Zhang San" };
let obj2 = { name: "Li Si" };
// Create an array
let array = [obj1, obj2];
// Delete an object
array.splice(0, 1);
// Trigger the young generation GC
ArkTools.hintGC();
// Trigger the old generation GC
ArkTools.hintOldSpaceGC();
Enter fullscreen mode Exit fullscreen mode

In the above code, we created two objects and an array and added them to the array. Then, we deleted the first element in the array. At this time, the reference count of the object obj1 becomes 0 and can be recycled by the young generation GC. Finally, we call the ArkTools.hintGC() and ArkTools.hintOldSpaceGC() methods to trigger the young generation and old generation GCs respectively.

Summary

The generational GC model of ArkTS effectively improves the efficiency of garbage collection by dividing the memory space into the young generation and the old generation and using different recycling algorithms for optimization. By understanding the principles and optimization strategies of the generational GC, we can better manage memory resources and improve the performance of applications.

Top comments (0)