暂无描述
leak_detector
本项目基于 leak_detector 开发。
1. 安装与使用
1.1 安装方式
进入到工程目录并在 pubspec.yaml 中添加以下依赖:
pubspec.yaml
...
dependencies:
leak_detector:
git:
url: https://gitcode.com/openharmony-sig/flutter_leak_detector.git
# ref: 1.1.0-ohos.1.0.0-beta.1
ref: TAG # 请根据下方TAG版本对应表选择TAG
...
执行命令
flutter pub get
TAG 版本对应表
| Flutter 框架版本 | TAG | 分支 |
|---|---|---|
| 3.7 | 1.1.0-ohos.1.0.0-beta.1 | master |
| 3.22 | 1.1.0-ohos.1.0.0-beta.1 | master |
| 3.27 | 1.1.0-ohos.1.0.0-beta.1 | master |
| 3.35 | 1.1.0-ohos.1.0.0-beta.1 | master |
1.2 使用案例
使用案例详见 example。
2. 约束与限制
2.1 兼容性
在以下版本中已测试通过:
- Flutter: 3.7.12-ohos-1.1.3; SDK: 5.0.0(12); IDE: DevEco Studio: 5.1.0.828; ROM: 5.1.0.130 SP8;
- Flutter: 3.22.1-ohos-1.0.3; SDK: 5.0.0(12); IDE: DevEco Studio: 5.1.0.828; ROM: 5.1.0.130 SP8;
- Flutter: 3.27.5-ohos-1.0.1; SDK: 5.0.0(12); IDE: DevEco Studio: 6.0.1.251; ROM: 6.0.0.115 SP16;
- Flutter: 3.35.8-ohos-0.0.1; SDK: 6.0.1(21); IDE: DevEco Studio: 6.0.1.260; ROM: 6.0.0.120 SP6;
3. API
"ohos Support"列为 yes 表示 ohos 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
LeakNavigatorObserver
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| checkLeakDelay | 延迟检查泄漏的默认时间 | int | / | / | yes |
| shouldCheck | 回调判断路由是否加入检测 | ShouldAddedRoute | Route route | bool | yes |
| didPop | 当路由弹出时触发 | function | Route route, Route? previousRoute | void | yes |
| didPush | 当路由压栈时触发 | function | Route route, Route? previousRoute | void | yes |
| didRemove | 当路由移除时触发 | function | Route route, Route? previousRoute | void | yes |
| didReplace | 当路由替换时触发 | function | {Route? newRoute, Route? oldRoute} | void | yes |
LeakDetector
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| maxRetainingPath | VmService.getRetainingPath 限制 | static int? | / | / | yes |
| onLeakedStream | 内存泄漏事件流 | Stream | / | / | yes |
| onEventStream | 检测过程事件流 | Stream | / | / | yes |
| init | 初始化配置 | function | {int maxRetainingPath = 300} | void | yes |
| ensureReleaseAsync | 异步检测释放 | function | String? group, {int delay = 0} | Future | yes |
| addWatchObject | 添加监控对象 | function | Object obj, String group | void | yes |
StateLeakMixin
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| checkLeakDelayMill | 获取延迟时间 | getter | / | int | yes |
| watchGroup | 获取监控组标识 | getter | / | String | yes |
| initState | 初始化生命周期 | function | / | void | yes |
| dispose | 销毁生命周期 | function | / | void | yes |
LeakPreviewPage
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| leakInfoList | 存储泄漏信息列表 | final List | / | / | yes |
| showLeakedInfoPage | 显示单个泄漏详情 | function | BuildContext context, LeakedInfo leakInfo | void | yes |
| showLeakedInfoListPage | 显示泄漏列表 | function | BuildContext context, List leakInfoList | void | yes |
LeakedRecordStore
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| getAll | 获取所有泄漏记录 | function | / | Future<List> | yes |
| clear | 清空存储 | function | / | void | yes |
| deleteById | 按 ID 删除记录 | function | int id | void | yes |
| addAll | 批量添加记录 | function | List list | void | yes |
| add | 添加单条记录 | function | LeakedInfo info | void | yes |
LeakedInfo
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| retainingPath | 引用链路径 | List | / | / | yes |
| gcRootType | GC 根类型 | String? | / | / | yes |
| timestamp | 检测时间戳 | int? | / | / | yes |
| isNotEmpty | 判断引用链是否非空 | getter | / | bool | yes |
| retainingPathJson | 将引用链转为 JSON 字符串 | getter | / | String | yes |
4. 属性
"ohos Support"列为 yes 表示 ohos 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
DetectorEventType
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| addObject | 添加对象事件 | enum | / | / | yes |
| check | 检测事件 | enum | / | / | yes |
| startGC | 开始 GC 事件 | enum | / | / | yes |
| endGc | 结束 GC 事件 | enum | / | / | yes |
| startAnalyze | 开始分析事件 | enum | / | / | yes |
| endAnalyze | 结束分析事件 | enum | / | / | yes |
DetectorEvent
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| type | 事件类型 | DetectorEventType | / | / | yes |
| data | 事件携带数据 | dynamic | / | / | yes |
RetainingNode
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| clazz | 类名 | String | / | / | yes |
| parentField | 父节点字段名 | String? | / | / | yes |
| libraries | 库名称 | String? | / | / | yes |
| string | 对象字符串表示 | String? | / | / | yes |
| parentKey | Map 类型父节点键值 | String? | / | / | yes |
| parentIndex | 列表类型父节点索引 | int? | / | / | yes |
| sourceCodeLocation | 源码位置信息 | SourceCodeLocation? | / | / | yes |
| closureInfo | 闭包信息 | ClosureInfo? | / | / | yes |
| leakedNodeType | 泄漏节点类型 | LeakedNodeType | / | / | yes |
| important | 是否为重要节点 | bool | / | / | yes |
SourceCodeLocation
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| code | 源码片段 | String? | / | / | yes |
| lineNum | 行号 | int? | / | / | yes |
| columnNum | 列号 | int? | / | / | yes |
| className | 类名 | String? | / | / | yes |
| uri | 文件路径 | String? | / | / | yes |
ClosureInfo
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| closureFunctionName | 闭包函数名 | String? | / | / | yes |
| closureOwner | 闭包所有者(类/方法) | String? | / | / | yes |
| closureOwnerClass | 闭包所属类 | String? | / | / | yes |
| libraries | 所属库 | String? | / | / | yes |
| funLine | 函数起始行号 | int? | / | / | yes |
| funColumn | 函数起始列号 | int? | / | / | yes |
5. 遗留问题
6. 其他
7. 开源协议
本项目基于 The BSD-2-Clause License,请自由地享受和参与开源。