Is there memory issue with isolate in flutter app?

前端 未结 2 895
既然无缘
既然无缘 2021-01-17 14:00

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(         


        
相关标签:
2条回答
  • 2021-01-17 14:03

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

    The run a simplified version of your sample:

    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.

    0 讨论(0)
  • 2021-01-17 14:22

    For a starter like us, we need to understand that compute function is nothing but the isolate itself. and the more you call to create isolate the more memory you will be needing. This reference Isolates spawn will take ~ 2mb of ram and so we need to make isolates as less as possible even though you might say that I'm just computing and returning result so isolate might get GC call but no at a time you might be scrolling and caching or doing something with isolate or your code within that isolate can impact huge in-memory footprint.

    so rather doing that I suggest you create one isolate and do whatever stuff you want to do and when you finish all you copy faces then close the isolate.

    watch this video also to know how to use isolate

    0 讨论(0)
提交回复
热议问题