• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用JSVM-API进行内存管理
2<!--Kit: NDK Development-->
3<!--Subsystem: arkcompiler-->
4<!--Owner: @yuanxiaogou; @string_sz-->
5<!--Designer: @knightaoko-->
6<!--Tester: @test_lzz-->
7<!--Adviser: @fang-jinxu-->
8
9## 简介
10
11JSVM-API提供了一组用于管理JavaScript虚拟机内存的API,可以更好地控制JavaScript代码使用的内存,并优化内存管理和垃圾回收机制。
12
13## 基本概念
14
15在JavaScript中,内存管理和垃圾回收是自动进行的。JavaScript虚拟机负责跟踪对象的分配和释放,并在必要时回收不再使用的内存。但是,在某些情况下,JSVM可能会消耗大量的内存,这可能会导致内存不足的错误。为了避免这种情况,JSVM-API提供了一些接口,以便更好地控制内存管理和垃圾回收机制。
16
17## 接口说明
18
19| 接口                       | 功能说明                            |
20|----------------------------|-------------------------------------|
21| OH_JSVM_AdjustExternalMemory         | 用于管理由JavaScript对象持有的外部分配内存|
22| OH_JSVM_MemoryPressureNotification   | 通知虚拟机系统内存不足并有选择地触发垃圾回收|
23
24## 使用示例
25
26JSVM-API接口开发流程请参考[使用JSVM-API实现JS与C/C++语言交互开发流程](use-jsvm-process.md)。本文仅展示接口对应C++及ArkTS相关代码。
27
28### OH_JSVM_AdjustExternalMemory
29
30设置JavaScript对象保持活动状态的外部分配内存的数量
31
32cpp部分代码:
33
34```cpp
35// hello.cpp
36#include "napi/native_api.h"
37#include "ark_runtime/jsvm.h"
38#include <hilog/log.h>
39// OH_JSVM_AdjustExternalMemory的样例方法
40static JSVM_Value AdjustExternalMemory(JSVM_Env env, JSVM_CallbackInfo info)
41{
42    // 分配1MB的内存
43    int64_t change = 1024 * 1024;
44    int64_t adjustedValue = 0;
45    JSVM_Status status = OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue);
46    if (status != JSVM_OK) {
47        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_AdjustExternalMemory: failed");
48    } else {
49        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_AdjustExternalMemory: success");
50        OH_LOG_INFO(LOG_APP, "JSVM Allocate memory size: %{public}d", adjustedValue);
51    }
52    JSVM_Value checked;
53    OH_JSVM_GetBoolean(env, true, &checked);
54    return checked;
55}
56// AdjustExternalMemory注册回调
57static JSVM_CallbackStruct param[] = {
58    {.data = nullptr, .callback = AdjustExternalMemory},
59};
60static JSVM_CallbackStruct *method = param;
61// AdjustExternalMemory方法别名,供JS调用
62static JSVM_PropertyDescriptor descriptor[] = {
63    {"adjustExternalMemory", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
64};
65```
66
67样例测试JS
68
69```c++
70const char *srcCallNative = R"JS(adjustExternalMemory())JS";
71```
72<!-- @[oh_jsvm_adjust_external_memory](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/hello.cpp) -->
73输出结果:
74在LOG中输出以下信息:
75```cpp
76JSVM OH_JSVM_AdjustExternalMemory: success
77JSVM Allocate memory size: 1048576
78```
79### OH_JSVM_MemoryPressureNotification
80
81使用OH_JSVM_MemoryPressureNotification通知虚拟机系统内存不足并有选择地触发垃圾回收。
82
83cpp部分代码:
84
85```cpp
86// hello.cpp
87#include "napi/native_api.h"
88#include "ark_runtime/jsvm.h"
89#include <hilog/log.h>
90// OH_JSVM_MemoryPressureNotification的样例方法
91static JSVM_Value MemoryPressureNotification(JSVM_Env env, JSVM_CallbackInfo info)
92{
93    // 设置当前JSVM的内存压力级别
94    JSVM_Status status = OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
95    if (status != JSVM_OK) {
96        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_MemoryPressureNotification: failed");
97    } else {
98        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_MemoryPressureNotification: success");
99        OH_LOG_INFO(LOG_APP, "JSVM Current JSVM memory pressure level: %{public}d", JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
100    }
101    return nullptr;
102}
103// MemoryPressureNotification注册回调
104static JSVM_CallbackStruct param[] = {
105    {.data = nullptr, .callback = MemoryPressureNotification},
106};
107static JSVM_CallbackStruct *method = param;
108// MemoryPressureNotification方法别名,供JS调用
109static JSVM_PropertyDescriptor descriptor[] = {
110    {"memoryPressureNotification", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
111};
112```
113
114样例测试JS
115
116```c++
117const char *srcCallNative = R"JS(memoryPressureNotification())JS";
118```
119<!-- @[oh_jsvm_memory_pressure_notification](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/hello.cpp) -->
120输出结果:
121在LOG中输出以下信息:
122```cpp
123JSVM OH_JSVM_MemoryPressureNotification: success
124JSVM Current JSVM memory pressure level: 2
125```