文档管理中心
指南NDK开发代码开发使用JSVM-API实现JS与C/C++语言交互JSVM-API使用指导使用JSVM-API进行内存管理

使用JSVM-API进行内存管理

简介

JSVM-API提供了一组用于管理JavaScript虚拟机内存的API,可以更好地控制JavaScript代码使用的内存,并优化内存管理和垃圾回收机制。

基本概念

在JavaScript中,内存管理和垃圾回收是自动进行的。JavaScript虚拟机负责跟踪对象的分配和释放,并在必要时回收不再使用的内存。但是,在某些情况下,JSVM可能会消耗大量的内存,这可能会导致内存不足的错误。为了避免这种情况,JSVM-API提供了一些接口,以便更好地控制内存管理和垃圾回收机制。

接口说明

展开
接口 功能说明
OH_JSVM_AdjustExternalMemory 用于管理由JavaScript对象持有的外部分配内存
OH_JSVM_MemoryPressureNotification 通知虚拟机系统内存不足并有选择地触发垃圾回收

使用示例

JSVM-API接口开发流程请参考使用JSVM-API实现JS与C/C++语言交互开发流程。本文仅展示接口对应C++及ArkTS相关代码。

OH_JSVM_AdjustExternalMemory

设置JavaScript对象保持活动状态的外部分配内存的数量

cpp部分代码:

收起
自动换行
深色代码主题
复制
  1. #include "napi/native_api.h"
  2. #include "ark_runtime/jsvm.h"
  3. #include "hilog/log.h"
  4. // ...
  5. // OH_JSVM_AdjustExternalMemory的样例方法
  6. static JSVM_Value AdjustExternalMemory(JSVM_Env env, JSVM_CallbackInfo info)
  7. {
  8. // 分配1MB的内存
  9. int64_t change = 1024 * 1024;
  10. int64_t adjustedValue = 0;
  11. JSVM_Status status = OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue);
  12. if (status != JSVM_OK) {
  13. OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_AdjustExternalMemory: failed");
  14. } else {
  15. OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_AdjustExternalMemory: success");
  16. OH_LOG_INFO(LOG_APP, "JSVM Allocate memory size: %{public}d", adjustedValue);
  17. }
  18. JSVM_Value checked;
  19. OH_JSVM_GetBoolean(env, true, &checked);
  20. return checked;
  21. }
  22. // AdjustExternalMemory注册回调
  23. static JSVM_CallbackStruct param[] = {
  24. {.data = nullptr, .callback = AdjustExternalMemory},
  25. };
  26. static JSVM_CallbackStruct *method = param;
  27. // AdjustExternalMemory方法别名,供JS调用
  28. static JSVM_PropertyDescriptor descriptor[] = {
  29. {"adjustExternalMemory", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
  30. };
  31. const char *SRC_CALL_NATIVE = R"JS(adjustExternalMemory())JS";

样例测试JS

收起
自动换行
深色代码主题
复制
  1. const char *srcCallNative = R"JS(adjustExternalMemory())JS";

输出结果:

在LOG中输出以下信息:

收起
自动换行
深色代码主题
复制
  1. JSVM OH_JSVM_AdjustExternalMemory: success
  2. JSVM Allocate memory size: 1048576

OH_JSVM_MemoryPressureNotification

使用OH_JSVM_MemoryPressureNotification通知虚拟机系统内存不足并有选择地触发垃圾回收。

cpp部分代码:

收起
自动换行
深色代码主题
复制
  1. #include "napi/native_api.h"
  2. #include "ark_runtime/jsvm.h"
  3. #include "hilog/log.h"
  4. // ...
  5. // OH_JSVM_MemoryPressureNotification的样例方法
  6. static JSVM_Value MemoryPressureNotification(JSVM_Env env, JSVM_CallbackInfo info)
  7. {
  8. // 设置当前JSVM的内存压力级别
  9. JSVM_Status status = OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
  10. if (status != JSVM_OK) {
  11. OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_MemoryPressureNotification: failed");
  12. } else {
  13. OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_MemoryPressureNotification: success");
  14. OH_LOG_INFO(LOG_APP, "JSVM Current JSVM memory pressure level: %{public}d",
  15. JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
  16. }
  17. return nullptr;
  18. }
  19. // MemoryPressureNotification注册回调
  20. static JSVM_CallbackStruct param[] = {
  21. {.data = nullptr, .callback = MemoryPressureNotification},
  22. };
  23. static JSVM_CallbackStruct *method = param;
  24. // MemoryPressureNotification方法别名,供JS调用
  25. static JSVM_PropertyDescriptor descriptor[] = {
  26. {"memoryPressureNotification", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
  27. };
  28. const char *SRC_CALL_NATIVE = R"JS(memoryPressureNotification())JS";

样例测试JS

收起
自动换行
深色代码主题
复制
  1. const char *srcCallNative = R"JS(memoryPressureNotification())JS";

输出结果:

在LOG中输出以下信息:

收起
自动换行
深色代码主题
复制
  1. JSVM OH_JSVM_MemoryPressureNotification: success
  2. JSVM Current JSVM memory pressure level: 2
在 指南 中进行搜索
请输入您想要搜索的关键词