• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.hiviewdfx.jsLeakWatcher (js泄露检测)
2
3<!--Kit: Performance Analysis Kit-->
4<!--Subsystem: HiviewDFX-->
5<!--Owner: @lu-tao-->
6<!--Designer: @martin-duan-->
7<!--Tester: @gcw_KuLfPSbe-->
8<!--Adviser: @foryourself-->
9
10本模块提供了监控js对象是否发生泄露的能力。
11
12> **说明:**
13>
14> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
15
16## 导入模块
17<!--code_no_check-->
18```js
19import { jsLeakWatcher } from '@kit.PerformanceAnalysisKit';
20```
21
22
23## jsLeakWatcher.enable
24
25enable(isEnable: boolean): void
26
27使能js对象泄露检测,默认关闭。
28
29**系统能力**:SystemCapability.HiviewDFX.HiChecker
30
31**参数:**
32
33| 参数名 | 类型 | 必填 | 说明 |
34| -------- | -------- | -------- | -------- |
35| isEnable | boolean | 是 | 是否使能jsLeakWatcher。true:使能jsleakwatcher;false:不使能jsleakwatcher。 |
36
37**示例:**
38
39```js
40jsLeakWatcher.enable(true);
41```
42
43
44## jsLeakWatcher.watch
45
46watch(obj: object, msg: string): void
47
48注册待检测泄露的对象。
49
50**系统能力**:SystemCapability.HiviewDFX.HiChecker
51
52**参数:**
53
54| 参数名 | 类型 | 必填 | 说明 |
55| -------- | -------- | -------- | -------- |
56| obj | object | 是 | 需要检测的对象名。 |
57| msg | string | 是 | 自定义对象信息。 |
58
59**示例:**
60
61```js
62let obj:Object = new Object();
63jsLeakWatcher.watch(obj, "Trace Object");
64```
65
66
67## jsLeakWatcher.check
68
69check(): string
70
71获取已通过jsLeakWatcher.watch注册且可能发生泄露的对象列表,触发GC后未被回收的对象会被标记为泄露。
72
73**系统能力**:SystemCapability.HiviewDFX.HiChecker
74
75**返回值:**
76
77| 类型    | 说明                                                       |
78| ------- | ---------------------------------------------------------- |
79| string | JSON格式的疑似泄漏对象列表。 |
80
81**示例:**
82```js
83let leakObjlist:string = jsLeakWatcher.check();
84```
85
86
87## jsLeakWatcher.dump
88
89dump(filePath: string): Array&lt;string&gt;
90
91导出泄漏列表和虚拟机内存快照。
92
93**系统能力**:SystemCapability.HiviewDFX.HiChecker
94
95**参数:**
96
97| 参数名 | 类型 | 必填 | 说明 |
98| -------- | -------- | -------- | -------- |
99| filePath | string | 是 | 导出信息生成的文件存放的路径。 |
100
101**返回值:**
102
103| 类型    | 说明                                                       |
104| ------- | ---------------------------------------------------------- |
105| Array&lt;string&gt; | 导出结果的数组。索引0为泄露列表文件名,后缀为.jsleaklist;索引1为虚拟机内存快照文件名,后缀为.heapsnapshot。 |
106
107**错误码:**
108
109以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
110
111| 错误码ID | 错误信息 |
112| ------- | ----------------------------------------------------------------- |
113| 401 | Parameter error. The filepath is invalid.                      |
114
115**示例:**
116<!--code_no_check-->
117```js
118let context = this.getUIContext().getHostContext();
119let files: Array<string> = jsLeakWatcher.dump(context?.filesDir);
120```
121
122
123## jsLeakWatcher.enableLeakWatcher<sup>20+</sup>
124
125enableLeakWatcher(isEnabled: boolean, configs: Array&lt;string&gt;, callback: Callback&lt;Array&lt;string&gt;&gt;): void
126
127使能js对象泄露检测,默认关闭。
128
129这个接口通过一次调用即可检测JS对象的内存泄漏,比之前需要调用四个函数(enable、watch、check、dump)的方法更加简洁。
130
131如果发生内存泄漏,泄漏文件将通过回调函数返回给开发者。
132
133
134**系统能力**:SystemCapability.HiviewDFX.HiChecker
135
136**参数:**
137
138| 参数名 | 类型 | 必填 | 说明 |
139| -------- | -------- | -------- | -------- |
140| isEnabled | boolean | 是| 是否使能js对象内存泄漏检测功能。true:开启该功能;false:关闭该功能。|
141| configs | Array&lt;string&gt; | 是| 配置项,数组中每个元素为监测具体对象的类型。<br>可配置项包括:XComponent,NodeContainer,Window,Custom Component和Ability。 |
142| callback | Callback&lt;Array&lt;string&gt;&gt; | 是| 回调函数,用于接收jsLeakWatcher.enableLeakWatcher接口的返回的内存泄漏的对象。<br>回调函数中传入一个数组对象,索引0为泄露列表文件名,后缀为.jsleaklist;索引1为虚拟机内存快照文件名,后缀为.rawheap。|
143
144
145**错误码:**
146
147以下错误码的详细介绍请参见[JsLeakWatcher错误码](./errorcode-jsleakwatcher.md)。
148
149| 错误码ID| 错误信息|
150| ------- | ----------------------------------------------------------------- |
151| 10801001 | The parameter isEnabled is invalid.                              |
152| 10801002 | The parameter config is invalid.                                 |
153| 10801003 | The parameter callback is invalid. Input parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. |
154
155**示例:**
156
157<!--code_no_check-->
158```ts
159let config: Array<string> = ['XComponent'];
160// 监测js对象XComponent的内存泄漏
161jsLeakWatcher.enableLeakWatcher(true, config, (filePath: Array<string>) => {
162    console.info('JsLeakWatcher leaklistFileName:' + filePath[0]);
163    console.info('JsLeakWatcher heapDumpFileName:' + filePath[1]);
164});
165```
166
167