ensureReleaseAsync method

dynamic ensureReleaseAsync(
  1. String? group, {
  2. int delay = 0,
})

Start to detect whether there is a memory leak

Implementation

ensureReleaseAsync(String? group, {int delay = 0}) async {
  Expando? expando = _watchGroup[group];
  _watchGroup.remove(group);
  if (expando != null) {
    //延时检测,有些state会在页面退出之后延迟释放,这并不表示就一定是内存泄漏。
    //比如runZone就会延时释放
    Timer(Duration(milliseconds: delay), () async {
      // add a check task
      _checkTaskQueue.add(
        DetectorTask(
          expando,
          sink: _onEventStreamController.sink,
          onStart: () => _onEventStreamController
              .add(DetectorEvent(DetectorEventType.check, data: group)),
          onResult: () {
            _currentTask = null;
            _checkStartTask();
          },
          onLeaked: (LeakedInfo? leakInfo) {
            //notify listeners
            if (leakInfo != null && leakInfo.isNotEmpty) {
              _onLeakedStreamController.add(leakInfo);
            }
          },
        ),
      );
      expando = null;
      _checkStartTask();
    });
  }
}