flutter_tflite icon indicating copy to clipboard operation
flutter_tflite copied to clipboard

Memory leaks

Open mike38217 opened this issue 3 years ago • 4 comments

I'm trying to use the example project. When I use different models I see that memory is not being release after close() method is called (iOS 14). If I use my custom model (64 MB) app gets crashed after 2 runs on device because it out of memory. Does anyone have any idea how to fix it?

mike38217 avatar May 04 '21 11:05 mike38217

I think the issue is that interpreter.release() is not being called causing the memory to be allocated https://github.com/shaqian/flutter_tflite/blob/ce8075a391e02e0cc0bc6c2db950aa961a17d745/ios/Classes/TflitePlugin.mm#L1488

AbdulRashidReshamwala avatar Jul 24 '21 17:07 AbdulRashidReshamwala

This change seems to work.

void close() {
#ifdef TFLITE2
+  if (interpreter != nullptr) {
+    TfLiteInterpreterDelete(interpreter);
+  }
  interpreter = nullptr;
  if (delegate != nullptr)
    TFLGpuDelegateDelete(delegate);
  delegate = nullptr;

However, in ios, there is a memory leak even in inference processing. Does anyone know any solution tips?

yokoi-engineering avatar Aug 04 '21 07:08 yokoi-engineering

@yokoi-engineering can you point me where the memory leak is occurring i can take a look

AbdulRashidReshamwala avatar Feb 26 '22 20:02 AbdulRashidReshamwala

interpreter and options (in loadModel func) and model and 'delegate'

Now that I'm away from work, I'll paste the code.

void close() {
#ifdef TFLITE2
  if (interpreter != nullptr) {
    TfLiteInterpreterDelete(interpreter);
  }
  interpreter = nullptr;
  if (options != nullptr) {
    TfLiteInterpreterOptionsDelete(options);
  }
  options = nullptr;
  if (model != nullptr) {
    TfLiteModelDelete(model);
  }
  model = nullptr;
  if (delegate != nullptr) {
    TFLGpuDelegateDelete(delegate);
  }
  delegate = nullptr;
#else
  interpreter.release();
  interpreter = NULL;
#endif
  model = NULL;
  labels.clear();
}

options move to member.

yokoi-engineering avatar Mar 07 '22 05:03 yokoi-engineering