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
The memory management and garbage collection mechanism of HarmonyOS Next are crucial for the performance of applications. Reasonable configuration of GC parameters and memory tuning can significantly improve the running efficiency of applications. This article will introduce the functions of key GC parameters in ArkTS, configuration strategies, and best practices for memory tuning, helping developers create efficient and stable HarmonyOS applications.
Detailed Explanation of GC Parameter Configuration
ArkTS provides a rich set of GC parameters that can be used to adjust the allocation and recycling strategies of different memory spaces. The following are some key GC parameters and their functions:
- SemiSpaceSize: Controls the size of the young generation SemiSpace, affecting the frequency and efficiency of young generation GC.
- oldSpaceOvershootSize: Controls the overshoot size of the old generation space, affecting the frequency and efficiency of old generation GC.
- HeapSize: Controls the total size of the heap, affecting the memory footprint of the application.
- gcThreadNum: Controls the number of GC threads, affecting the parallelism and efficiency of GC.
- defaultReadOnlySpaceSize: Controls the size of the read-only space, affecting the storage of read-only data during runtime.
- defaultNonMovableSpaceSize: Controls the size of the non-movable space, affecting the storage of system class objects.
- defaultSnapshotSpaceSize: Controls the size of the snapshot space, affecting the generation efficiency of heap snapshots.
- defaultMachineCodeSpaceSize: Controls the size of the machine code space, affecting the storage of program machine code. ### Configuration File Explanation The application configuration file of HarmonyOS Next (such as ohosmanifest.json) contains the configuration of some GC parameters, such as heapSize and gcThreadNum. Developers can adjust them according to the requirements of the application:
- heapSize: Sets the total size of the heap and should be configured according to the actual memory requirements of the application.
- gcThreadNum: Sets the number of GC threads and can be adjusted according to the number of CPU cores of the application. ### Smart GC HarmonyOS Next provides a smart GC function that can dynamically adjust GC parameters according to the application scenario to avoid triggering GC in performance-sensitive scenarios, which may cause application stuttering. Smart GC is mainly targeted at the following scenarios:
- Application Cold Start: During application startup, smart GC will adjust GC parameters according to the memory footprint to avoid frequent GC.
- Application Scrolling: During application scrolling operations, smart GC will adjust GC parameters according to the frequency of user operations to avoid interfering with user operations.
- Application Click Page Jump: During application page jumps, smart GC will adjust GC parameters according to the time-consuming of page switching to avoid affecting the smoothness of page switching. ### Debugging and Optimization Developers can monitor the execution effect of GC by analyzing GC logs and optimize according to the log information. The following are some common GC log keywords:
- [gc]: Represents GC log.
- [HPP YoungGC]: Represents young generation GC.
- [HPP OldGC]: Represents old generation GC.
- [CompressGC]: Represents compress GC.
- IsInBackground: Indicates whether the application is running in the background.
- SensitiveStatus: Indicates whether it is in a performance-sensitive scenario. ### Table: Key Parameters and Recommended Configurations | Parameter | Function | Recommended Configuration | |---|---|---| | SemiSpaceSize | Controls the size of SemiSpace | 2MB - 16MB, adjust according to the application memory requirements and object allocation speed | | oldSpaceOvershootSize | Controls the overshoot size of OldSpace | 4MB - 8MB, adjust according to the application memory footprint | | HeapSize | Controls the total size of the heap | Configure according to the actual memory requirements of the application | | gcThreadNum | Controls the number of GC threads | Adjust according to the number of CPU cores of the application | | defaultReadOnlySpaceSize | Controls the size ofReadOnlySpace | 256KB | | defaultNonMovableSpaceSize | Controls the size of NonMovableSpace | 2MB | | defaultSnapshotSpaceSize | Controls the size of SnapshotSpace | 512KB | | defaultMachineCodeSpaceSize | Controls the size of MachineCodeSpace | 2MB | ### An Example The following sample code shows how to dynamically adjust GC parameters in ArkTS:
// Dynamically adjust the size of SemiSpace
ArkRuntimeConfig.setSemiSpaceSize(8);
// Dynamically adjust the overshoot size of OldSpace
ArkRuntimeConfig.setOldSpaceOvershootSize(16);
// Dynamically adjust the number of GC threads
ArkRuntimeConfig.setGcThreadNum(7);
In the above code, we call the ArkRuntimeConfig.setSemiSpaceSize()
, ArkRuntimeConfig.setOldSpaceOvershootSize()
, and ArkRuntimeConfig.setGcThreadNum()
methods respectively to dynamically adjust the size of SemiSpace, the overshoot size of OldSpace, and the number of GC threads.
Summary
Reasonable configuration of GC parameters and memory tuning are important means to improve the performance of HarmonyOS Next applications. By understanding the functions of key GC parameters, configuration strategies, and best practices for memory tuning, and adjusting according to the actual requirements of the application, we can create efficient and stable HarmonyOS applications.
Top comments (0)