Apparence-io / CamerAwesome

📸 Embedding a camera experience within your own app shouldn't be that hard. A flutter plugin to integrate awesome Android / iOS camera experience.
https://ApparenceKit.dev
MIT License
920 stars 208 forks source link

Memory keeps increasing and stacking up upon displaying and closing the camera preview multiple times #242

Closed GabrielAraujo closed 1 year ago

GabrielAraujo commented 1 year ago

Steps to Reproduce

If you keep displaying and closing the camera multiple times it will increase the RAM usage to a point where the app crashes due to OOM. Screen Shot 2023-02-06 at 11 00 58

As you can see in the image the spike is when you open the camera and when it drops upon closing it never comes back to the previous level of mem usage. And it is because of some memory retain that is happening on the call tree bellow Screen Shot 2023-02-06 at 11 02 29

I've found this thread but I don't know if it relates to how the plugin was implemented.

Due to the nature of our app, the users open and close the camera quite a lot and OOM issues is the main cause of crashes for us right now.

Expected results

App running smoothly

Actual results

App crashing

About your device

Brand Model OS
Apple iPhone X 13.6.1

g-apparence commented 1 year ago

Thank you for this great analyze. We will investigate further to reproduce and see what we can do.

GabrielAraujo commented 1 year ago

@g-apparence Thank you! I've also tested under the example project on this repo and the same happens.. So it is def something related to how we display the camera preview on the native side.

Let me know if you need any help!

Screen Shot 2023-02-06 at 14 03 03

jamesdixon commented 1 year ago

@GabrielAraujo nice find!

g-apparence commented 1 year ago

Checked on Android Seems ok Capture d’écran 2023-02-07 à 15 13 04

Going to try reproducing on some iOS phones now. I'll let you know

g-apparence commented 1 year ago

@g-apparence Thank you! I've also tested under the example project on this repo and the same happens.. So it is def something related to how we display the camera preview on the native side.

Let me know if you need any help!

Screen Shot 2023-02-06 at 14 03 03

Seems related yes... This seems like a big flutter issue that should get more attention

GabrielAraujo commented 1 year ago

I've tested using the Camera from the Image picker package and the same does not happen.. Could it be something this package is not properly handling/disposing? Due to the not released mem size it feels like it is some kind of image buffer that is not properly released..

Do you have any next steps in mind or are you planning on waiting for the flutter side?

jamesdixon commented 1 year ago

@g-apparence is there a specific Flutter issue on Github that you're referencing?

g-apparence commented 1 year ago

From the initial message

I've found https://github.com/flutter/flutter/issues/77621

I've some ideas, but I have to discuss first with our iOS expert as I'm not comfortable with ObjC.

g-apparence commented 1 year ago

I can't reproduce this on an Iphone X / iOS 16.3.

Maybe that's related to pre iOS 16 and textures. I've added an unregister texture before closing the videoon iOS, but I cannot garantee that's going to fix your bug.

GabrielAraujo commented 1 year ago

Screen Shot 2023-02-08 at 07 52 30 Even seems to be a bit worst and the memory don't return to a lower level at all.

Here is the gist on the changes I did on main.dart for the example project so we could simulate closing/reopening the camera. (Could also be something on the way I've implemented it too)

GabrielAraujo commented 1 year ago

The good news is that on the Fix you worked on it seems to solve the issue: Screen Shot 2023-02-08 at 08 08 22

Here is the mem profile after opening/closing the camera multiple times on the fix/ios_unregister_texture branch

g-apparence commented 1 year ago

Ahh great.

I think it's related on the fact that you are using a constant Key on the camera page. Flutter tries to recreate the same widget without the texture properly disposed. Just out of curiosity, I'll try to see if that's right.

g-apparence commented 1 year ago

I close this as it seems fixed. Thank you for your help fixing this. 🙏🏻

jnarowski commented 1 year ago

Thank you for fixing this! Do you have an idea when this will be released as an official version increment to the package?