• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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// AdjustExternalMemory注册回调
34static JSVM_CallbackStruct param[] = {
35    {.data = nullptr, .callback = AdjustExternalMemory},
36};
37static JSVM_CallbackStruct *method = param;
38// AdjustExternalMemory方法别名,供JS调用
39static JSVM_PropertyDescriptor descriptor[] = {
40    {"adjustExternalMemory", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
41};
42// OH_JSVM_AdjustExternalMemory的样例方法
43static JSVM_Value AdjustExternalMemory(JSVM_Env env, JSVM_CallbackInfo info)
44{
45    // 分配1MB的内存
46    int64_t change = 1024 * 1024;
47    int64_t adjustedValue = 0;
48    JSVM_Status status = OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue);
49    if (status != JSVM_OK) {
50        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_AdjustExternalMemory: failed");
51    } else {
52        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_AdjustExternalMemory: success");
53        OH_LOG_INFO(LOG_APP, "JSVM Allocate memory size: %{public}d", adjustedValue);
54    }
55    JSVM_Value checked;
56    OH_JSVM_GetBoolean(env, true, &checked);
57    return checked;
58}
59```
60
61ArkTS侧示例代码
62
63```ts
64import hilog from "@ohos.hilog"
65// 通过import的方式,引入Native能力。
66import napitest from "libentry.so"
67let script: string = `
68   adjustExternalMemory()
69  `;
70try {
71  let result = napitest.runJsVm(script);
72  hilog.info(0x0000, 'JSVM', 'adjustExternalMemory:%{public}s', result);
73} catch (error) {
74  hilog.error(0x0000, 'JSVM', 'adjustExternalMemory: %{public}s', error.message);
75}
76```
77
78### OH_JSVM_MemoryPressureNotification
79
80通知虚拟机系统内存不足并有选择地触发垃圾回收
81
82cpp部分代码
83
84```cpp
85// hello.cpp
86#include "napi/native_api.h"
87#include "ark_runtime/jsvm.h"
88#include <hilog/log.h>
89// MemoryPressureNotification注册回调
90static JSVM_CallbackStruct param[] = {
91    {.data = nullptr, .callback = MemoryPressureNotification},
92};
93static JSVM_CallbackStruct *method = param;
94// MemoryPressureNotification方法别名,供JS调用
95static JSVM_PropertyDescriptor descriptor[] = {
96    {"memoryPressureNotification", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
97};
98// OH_JSVM_MemoryPressureNotification的样例方法
99static JSVM_Value MemoryPressureNotification(JSVM_Env env, JSVM_CallbackInfo info)
100{
101    // 设置当前JSVM的内存压力级别
102    JSVM_Status status = OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
103    if (status != JSVM_OK) {
104        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_MemoryPressureNotification: failed");
105    } else {
106        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_MemoryPressureNotification: success");
107        OH_LOG_INFO(LOG_APP, "JSVM Current JSVM memory pressure level: %{public}d", JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
108    }
109    return nullptr;
110}
111```
112
113ArkTS侧示例代码
114
115```ts
116import hilog from "@ohos.hilog"
117// 通过import的方式,引入Native能力。
118import napitest from "libentry.so"
119let script: string = `
120   memoryPressureNotification();
121  `;
122try {
123  let result = napitest.runJsVm(script);
124  hilog.info(0x0000, 'JSVM', 'memoryPressureNotification:%{public}s', result);
125} catch (error) {
126  hilog.error(0x0000, 'JSVM', 'memoryPressureNotification:%{public}s', error.message);
127}
128```
129