DEV Community

SameX
SameX

Posted on

A Detailed Explanation of ArkTS Memory Space: From SemiSpace to HugeObjectSpace

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

As the development language of the HarmonyOS system, ArkTS provides a rich memory space management mechanism to meet the memory requirements in different scenarios. This article will explain in detail the memory space structure of ArkTS, including SemiSpace, OldSpace, HugeObjectSpace, ReadOnlySpace, NonMovableSpace, SnapshotSpace, and MachineCodeSpace, and introduce how to optimize the use of these spaces to improve application performance.

SemiSpace Memory Space

SemiSpace is part of the young generation space and is used to store newly created objects. The young generation space is divided into two semi-spaces (SemiSpace), which are respectively used for object creation and recycling. When the space of one of the semi-spaces is insufficient, the young generation GC will be triggered, and the surviving objects will be copied to the other semi-space, and the old semi-space will be recycled.

OldSpace and HugeObjectSpace

OldSpace is used to store objects with a relatively long survival time and a relatively high survival rate. The old generation GC mainly uses algorithms such as Sweep and Compact to clean and compress the old generation space.
HugeObjectSpace is used to store large objects, such as large arrays. The large object space is managed in a separate area to ensure the performance of large objects.

Read-Only Space and Non-Movable Space

ReadOnlySpace is used to store read-only data during runtime, such as string constants. The memory space of ReadOnlySpace will not be recycled by the garbage collector, so it needs to be used with caution.
NonMovableSpace is used to store immovable objects, such as system class objects. The memory space of NonMovableSpace will not be moved by the garbage collector, so it needs to be used with caution.

Snapshot and Machine Code Space

SnapshotSpace is used for the space when dumping heap snapshots. Heap snapshots can help developers analyze memory usage and object relationships.
MachineCodeSpace is used to store the machine code of the program. The memory space of MachineCodeSpace will not be recycled by the garbage collector, so it needs to be used with caution.

Configuration Parameters and Optimization Options for Different Spaces

Space Type Configuration Parameter Function Optimization Option
SemiSpace semiSpaceSize, semiSpaceTriggerConcurrentMark, semiSpaceStepOvershootSize Control the size of SemiSpace and the GC trigger condition Adjust the parameters according to application requirements to improve the recycling efficiency
OldSpace oldSpaceOvershootSize Control the overshoot size of OldSpace Adjust the parameter according to the memory usage to avoid frequent GC
ReadOnlySpace defaultReadOnlySpaceSize Control the size of ReadOnlySpace Use with caution to avoid memory leaks
NonMovableSpace defaultNonMovableSpaceSize Control the size of NonMovableSpace Use with caution to avoid memory leaks
SnapshotSpace defaultSnapshotSpaceSize Control the size of SnapshotSpace Adjust the parameter according to needs to optimize the snapshot generation efficiency
MachineCodeSpace defaultMachineCodeSpaceSize Control the size of MachineCodeSpace Use with caution to avoid memory leaks

An Example

The following sample code shows how to specify and optimize different memory spaces in ArkTS:

// Set the size of SemiSpace
ArkRuntimeConfig.setSemiSpaceSize(8);
// Trigger the young generation GC
ArkTools.hintGC();
// Set the overshoot size of OldSpace
ArkRuntimeConfig.setOldSpaceOvershootSize(16);
// Trigger the old generation GC
ArkTools.hintOldSpaceGC();
// Specify to use the large object space
let array = new Int32Array(1024 * 1024); // Create a 1MB array
// Specify to use the read-only space
let constant = "This is a constant string";
Enter fullscreen mode Exit fullscreen mode

In the above code, we set the size of SemiSpace through the ArkRuntimeConfig.setSemiSpaceSize() method and trigger the young generation GC through the ArkTools.hintGC() method. We also set the overshoot size of OldSpace through the ArkRuntimeConfig.setOldSpaceOvershootSize() method and trigger the old generation GC through the ArkTools.hintOldSpaceGC() method. In addition, we create a large array and specify to use the large object space, and create a constant string and specify to use the read-only space.

Summary

ArkTS provides a rich memory space management mechanism, which can help developers optimize memory usage according to different scenarios and improve application performance. By understanding the characteristics and configuration parameters of different spaces, we can better manage memory resources, avoid memory leaks, and improve the stability of applications.

Top comments (0)