flutter应用程序中的隔离是否存在内存问题?

hoa*_*uyy 7 flutter

我对flutter应用程序的内存有问题,在使用计算时,我将这一行放在计算的函数参数中:

var image = imglib.Image.fromBytes(values[1].width, values[1].height, values[1].planes[0].bytes, format: imglib.Format.bgra);
Run Code Online (Sandbox Code Playgroud)

并以循环方式运行它,内存每次都会保持增长,然后内存不足,应用崩溃。

如果我没有那条线,则内存稳定在40mb。因此,我认为在计算中,在计算功能完成后并没有清除它。

有人有同样的问题吗?

编辑:

这就是我实现计算的方式:

image = await compute(getCropImage, [copyFaces, streamImg]);
Run Code Online (Sandbox Code Playgroud)

在getCropImage中:

Future<imglib.Image> getCropImage(List<dynamic> values) async {
  var image = imglib.Image.fromBytes(values[1].width, values[1].height, values[1].planes[0].bytes, format: imglib.Format.bgra);

  double topLeftX = values[0][0].boundingBox.topLeft.dx.round() -
  (values[0][0].boundingBox.width * 0.2);
  double topLeftY = values[0][0].boundingBox.topLeft.dy.round() -
  (values[0][0].boundingBox.height * 0.2);
  double width = values[0][0].boundingBox.width.round() +
  (values[0][0].boundingBox.width * 0.4);
  double height = values[0][0].boundingBox.height.round() +
  (values[0][0].boundingBox.height * 0.4);
  if (topLeftX <= 0) {
    topLeftX = 25;
  }
  if (topLeftY <= 0) {
    topLeftY = 25;
  }
  if ((topLeftX + width) >= values[1].width) {
    width = values[1].width - topLeftX - 25;
  }
  if ((topLeftY + height) >= values[1].height) {
    height = values[1].height - topLeftY - 25;
  }

  return imglib.copyCrop(
      image, topLeftX.round(), topLeftY.round(), width.round(), height.round());
}
Run Code Online (Sandbox Code Playgroud)

带有imglib的是Image包:

import 'package:image/image.dart' as imglib;
Run Code Online (Sandbox Code Playgroud)

每当我这样称呼时,记忆就在不断增长。

TWL*_*TWL 1

为了尝试使用您的示例进行重现,我必须首先从 ui.Image 进行转换:

Future<Uint8List> _bytePng(ui.Image image) async {
  ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
  Uint8List byteList = byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes);
  return byteList;
}
Run Code Online (Sandbox Code Playgroud)

运行示例的简化版本:

imglib.Image image2 = await compute(_getImage, [image1.width, image1.height, byteList]);


Future<imglib.Image> _getImage(List<dynamic> values) async {
  var temp = imglib.Image.fromBytes(values[0], values[1], values[2], format: imglib.Format.bgra);

  var rng = new Random().nextInt(50);
  imglib.Image cropped = imglib.copyCrop(temp, 0, 0, temp.width - rng, temp.height - rng);

  return cropped;
}
Run Code Online (Sandbox Code Playgroud)

但我没能看到记忆失控。所以你可能还有其他事情发生。


归档时间:

查看次数:

141 次

最近记录:

6 年,2 月 前