Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there memory issue with isolate in flutter app?

Tags:

flutter

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.

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?

Edit:

This is how I implement compute:

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

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

With imglib is the Image package:

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

Everytime I call this, the memory keep growing.

like image 864
hoangquyy Avatar asked Oct 28 '19 10:10

hoangquyy


People also ask

How do flutters prevent memory leaks?

Add LeakNavigatorObserver to navigatorObservers in MaterialApp , it will automatically detect whether there is a memory leak in the page's Widget and its corresponding Element object. If page's Widget is a StatefulWidget , it will also be automatically checked Its corresponding State .

What is isolate in Flutter?

What are Flutter isolates? An isolate is an abstraction on top of threads. It is similar to an event loop, with a few differences: An isolate has its own memory space. It cannot share mutable values with other isolates.

How do you check memory leak in Flutter app?

Step - 1 : Connect your device with android studio and run your application on your device. Step - 4 : To follow below steps as per screen shot , you can able to see object size and details. Which are caused memory leakages. First Select "Memory" from available menus than you can able to see below ui.

What is Dart isolate?

Using isolates, your Dart code can perform multiple independent tasks at once, using additional processor cores if they're available. Isolates are like threads or processes, but each isolate has its own memory and a single thread running an event loop.


1 Answers

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.

like image 148
TWL Avatar answered Sep 21 '22 19:09

TWL