am15h / object_detection_flutter

Truly realtime object-detection in flutter
188 stars 113 forks source link

Kill isolate when camera is disposed #31

Open batkhishign55 opened 2 years ago

batkhishign55 commented 2 years ago

Hi,

I found a problem while trying to implement the project in a different flutter app. When you close the camera and open again, there is a leftover process. If you do it over and over again, it will eventually fill up memory.

The problem is when camera is disposed, there's still an active isolate thread in the background. Therefore a simple fix was to kill the isolate on camera disposal.

espbee commented 2 years ago

@batkhishign55 I found that I had to do this as well. Quick side question-- have you found any memory leak issues on ios ? If you look at the runtime memory usage in XCode? I've been finding that when I dispose the Classifier / close the interpreter -- even in a test app with no camera view at all, no isolates, no actual inference -- the model doesn't seem to deallocate. Rather, the next time I run it, the memory usage drives higher, and so on, until eventually the app crashes.

Wondering if you've by any chance found anything similar? Or are your Classifier / interpreter / model clearing out totally when you close / dispose?

batkhishign55 commented 2 years ago

@espbee haven't tried it on ios because I don't have macos device yet. Will definitely look into it when I get a macbook in the future. If you find a solution, please share it with us.

espbee commented 2 years ago

@batkhishign55 i'm still not totally confident but the solution seems to be loading the model from file rather than asset-- if anyone is loading from asset. he's got a little block of commented description in the docs about saving the asset to a temp dir and loading the file from there-- it's how you'd do it anywhere in flutter. that approach seems to let the memory release, whereas when i loaded from asset directly, it did not.

For what it's worth,I'm not running the example app directly, more took inspiration about dealing with camera frames, and combined it with a slightly different isolate approach-- which comprises a part of the app I'm using it in.

However, I did test this memory issue in the basic Flutter Counter App and it happened there as well, just the same, using the stock SSD model that comes with this example app: open interpreter / close -> no release.

This is on XCode 13 or 12.5.

Anyway if anyone finds this and encounters the same, I can only suggest looking into the File rather than direct Asset model loading approach to initializing your interpreter.