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 high-performance partial garbage collection (HPP GC) model at runtime. Through hybrid algorithms and parallel and concurrent optimizations, it achieves efficient memory recycling and ensures the smooth operation of applications. This article will conduct an in-depth analysis of the HPP GC model of ArkTS to help developers better understand its principles and optimization strategies.
HPP GC Introduction
HPP GC (High Performance Partial Garbage Collection) is a garbage collection mechanism that combines partial garbage collection with multiple recycling algorithms. HPP GC mainly has the following characteristics:
- Generational Model: Divide the memory space into the young generation and the old generation, and adopt different recycling algorithms respectively.
- Hybrid Algorithm: Combine reference counting and object tracing algorithms to improve recycling efficiency.
- Parallel and Concurrent: Execute GC tasks in parallel to reduce the suspension time of the main thread. ### Differences between Young GC and Old GC Young GC and Old GC are the two main GC types of HPP GC, which are optimized for the memory characteristics and application scenarios of different generations:
- Young GC: Mainly recycles the young generation space, which is used to store newly created objects. Young GC mainly uses the Copying algorithm to copy the surviving objects to another semi-space and recycle the old semi-space. The triggering conditions of Young GC include: insufficient young generation space, reaching the preset threshold, etc. The characteristic of Young GC is that it is fast but recycles limited memory space.
- Old GC: Mainly recycles the old generation space, which is used to store objects with a long survival time. Old GC mainly uses algorithms such as Sweep and Compact to clean and compress the old generation space. The triggering conditions of Old GC include: insufficient old generation space, reaching the preset threshold, etc. The characteristic of Old GC is that it recycles more memory space but takes longer time. HPP GC dynamically adjusts the size and recycling threshold of the young generation and the old generation to balance the performance requirements of the foreground and the background and ensure the smooth operation of the application. ### CompressGC When the application switches to the background, HPP GC will trigger CompressGC. CompressGC will compress both the young generation and the old generation simultaneously to release memory space and optimize the performance when the application switches to the background. ### Performance Optimization HPP GC reduces the GC pause time and improves the application response speed through the following strategies:
- Concurrent Marking: Traverse the object graph concurrently for marking during the running of the application program to reduce the suspension time of the main thread.
- Parallel Recycling: Use multiple threads to execute garbage collection tasks in parallel to improve the recycling efficiency.
- Threshold Adjustment: Dynamically adjust the GC trigger threshold according to the running situation and memory usage of the application program to avoid frequent GC.
- Space Pre-allocation: Pre-allocate a certain amount of memory space when the application starts to avoid frequent memory allocation and recycling during runtime.
- Object Promotion Strategy: Promote objects from the young generation to the old generation according to the survival time of the objects to reduce the frequency of Young GC. ### Code Example The following sample code shows how to trigger HPP GC in ArkTS:
// Trigger Young GC
ArkTools.hintGC();
// Trigger Old GC
ArkTools.hintOldSpaceGC();
// Trigger CompressGC
ArkTools.hintCompressGC();
In the above code, we call the ArkTools.hintGC()
, ArkTools.hintOldSpaceGC()
, and ArkTools.hintCompressGC()
methods respectively to trigger Young GC, Old GC, and CompressGC.
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]: Indicates young GC, mainly recycling the young generation space.
- [HPP OldGC]: Indicates old GC, mainly recycling the old generation space.
- [CompressGC]: Indicates CompressGC, which compresses the entire heap space.
- IsInBackground: Indicates whether the application is running in the background.
- SensitiveStatus: Indicates whether it is in a performance-sensitive scenario.
- OnStartupEvent: 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 the application performance. ### Summary HPP GC is an efficient and reliable garbage collection mechanism in ArkTS. Through hybrid algorithms and parallel and concurrent optimizations, it achieves efficient memory recycling and ensures the smooth operation of applications. Developers can better manage memory resources, avoid memory leaks, and improve the stability of applications by understanding the principles and optimization strategies of HPP GC. By analyzing the GC logs, we need to have a deeper understanding of GC behavior and conduct targeted optimizations.
Top comments (0)