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