flutter应用程序中的隔离是否存在内存问题? [英] Is there memory issue with isolate in flutter app?

查看:136
本文介绍了flutter应用程序中的隔离是否存在内存问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem about memory with flutter app, when using compute, I put this line in the function parameter in compute:

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

并循环运行,每次内存持续增长,然后内存不足,应用崩溃.

And run it in loop, the memory keep growing everytime then out of memory and the app crashed.

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

If I don't have that line, the memory is stable at 40mb. So I think that in compute, it's not been cleaned up after the compute function finish.

有人遇到同样的问题吗?

Anyone have same problem?

这是我实现计算的方式:

This is how I implement compute:

image = await compute(getCropImage, [copyFaces, streamImg]);

在getCropImage中:

In 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());
}

带有imglib的是Image包:

With imglib is the Image package:

import 'package:image/image.dart' as imglib;

每次我这样称呼时,内存都在不断增长.

Everytime I call this, the memory keep growing.

推荐答案

要尝试重现您的示例,我必须先从ui.Image进行转换:

To try to reproduce with your sample, I had to convert from a ui.Image first:

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;
}

运行示例的简化版本:

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;
}

但是我无法看到内存失控.因此,您可能还有其他事情要发生.

But I wasn't able to see the memory go out of control. So you probably have something else going on.

这篇关于flutter应用程序中的隔离是否存在内存问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆