フラッターアプリのメモリに関する問題があり、computeを使用する場合は、computeの関数パラメーターに次の行を追加します。
var image = imglib.Image.fromBytes(values[1].width, values[1].height, values[1].planes[0].bytes, format: imglib.Format.bgra);
そしてそれをループで実行すると、メモリが毎回増え続け、その後メモリが不足し、アプリがクラッシュしました。
そのラインがない場合、メモリは40MBで安定しています。ですから、コンピューティングでは、コンピューティング関数の終了後はクリーンアップされていないと思います。
誰もが同じ問題を抱えていますか?
編集:
これは私がコンピューティングを実装する方法です:
image = await compute(getCropImage, [copyFaces, streamImg]);
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パッケージです。
import 'package:image/image.dart' as imglib;
これを呼び出すたびに、メモリは増え続けます。
var image
の1行目にgetCropImage(...)
そう使用してみて、使用後にリリースされていませんvar image
(常に新しいメモリを割り当てないようにするために)フィールドとして、おそらくすべてのループステップで新しいVARをインスタンス化しないように役立ちます!特に画像などの大きなオブジェクトで管理している場合は、常にこれらのタイプのオブジェクトを再利用するようにしてください。通常、ガベージコレクターは、未使用のオブジェクトをすべて解放することを保証しません。また、System.gc()
(メモリの割り当て解除を強制するために)直接または同様のメソッドを呼び出さないでください。これは、壊れたコードで最適化されていないコードの症状です。:)