openharmony 鸿蒙 js-apis-jsleakwatcher

2025-06-12 浏览 (1)

@ohos.hiviewdfx.jsLeakWatcher (JS Leak Watcher)

This module provides the capability of monitoring whether JS objects are leaked.

NOTE

The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { jsLeakWatcher } from '@kit.PerformanceAnalysisKit';

jsLeakWatcher.enable

enable(isEnable: boolean): void

Enables the detection for JS object leak. This function is disabled by default.

System capability: SystemCapability.HiviewDFX.HiChecker

Parameters

NameTypeMandatoryDescription
isEnablebooleanYesWhether to enable jsLeakWatcher.

Example

jsLeakWatcher.enable(true);

jsLeakWatcher.watch

watch(obj: object, msg: string): void

Registers the object to be checked.

System capability: SystemCapability.HiviewDFX.HiChecker

Parameters

NameTypeMandatoryDescription
objobjectYesName of the object to be checked.
msgstringYesCustom object information.

Example

let obj:Object = new Object();
jsLeakWatcher.watch(obj, "Trace Object");

jsLeakWatcher.check

check(): string

Obtains the list of objects that are registered using jsLeakWatcher.watch() and may leak. Objects that are not reclaimed after GC is triggered are marked as leaked.

System capability: SystemCapability.HiviewDFX.HiChecker

Return value

TypeDescription
stringList of objects that are suspected to leak, in JSON format.

Example

let leakObjlist:string = jsLeakWatcher.check();

jsLeakWatcher.dump

dump(filePath: string): Array<string>

Exports the list of leaked objects and VM memory snapshot.

System capability: SystemCapability.HiviewDFX.HiChecker

Parameters

NameTypeMandatoryDescription
filePathstringYesThe path for storing exported information files.

Return value

TypeDescription
Array<string>Array of exported results. Index 0 indicates the name of the leak list file, whose name extension is .jsleaklist. Index 1 indicates the name of the VM memory snapshot file, whose name extension is .heapsnapshort.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. The filepath is invalid.

Example

let context = this.getUIContext().getHostContext();
let files: Array<string> = jsLeakWatcher.dump(context?.filesDir);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Performance Analysis Kit

harmony 鸿蒙Performance Analysis Kit

harmony 鸿蒙HiAppEvent

harmony 鸿蒙HiAppEvent_AppEventGroup

harmony 鸿蒙HiAppEvent_AppEventInfo

harmony 鸿蒙HiCollie

harmony 鸿蒙HiCollie_DetectionParam

harmony 鸿蒙HiCollie_SetTimerParam

harmony 鸿蒙HiDebug

harmony 鸿蒙HiDebug_MemoryLimit

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/2UisF6