Memory leak on official CameraX sample

501 views
Skip to first unread message
Assigned to scot...@google.com by eri...@google.com

Rahmi Cemre Ünal

unread,
Jan 7, 2022, 7:52:08 PM1/7/22
to Android CameraX Discussion Group
Hi,

 I was using CameraX on our current project and getting some ridiculous crashes at  fragments that implements camera preview use case, also noticed that Android studio profiler tool gives memory leak warning for those fragments. So I tried to test it on official sample and I'm experiencing same leak also in this official camerax sample. My test devices are Samsung galaxy a01 and Samsung m11.

To reproduce this leak, you just need to change this line to app:popUpToInclusive="false" so when back button pressed, a fragment should be exist to prevent closing the application.

After changing that line, launch the app, give camera permissions, it will navigate to CameraFragment, then just press the back button. At this point, the fragment must be completely destroyed and the usecases expected to be unbinded. After all of that, if you take a look at the profiler and capture the heap dump, surprisingly, you will see CameraFragment is not destroyed and android studio shows a warning about the leak.

In the example code I just changed a single line on the nav_graph as I mentioned, nothing changed on camera implementation logic. Actually, this is a scenario that can be repeated with various examples. I see that the fragment remains in memory in every scenario where I just use the bindToLifecycle() method with the camera preview use case and then try to pop the fragment.

Is it possible to use the preview use case without a leak? I would appreciate it if you could help.


Scott Nien

unread,
Jan 9, 2022, 11:30:02 PM1/9/22
to Rahmi Cemre Ünal, Android CameraX Discussion Group
Not sure if this relates to the app:popUpToInclusive.  
I can reproduce it by using app:popUpToInclusive = true and launch app / back multiple times.  But the problem still remains even if I remove all the CameraX / camera related codes in CameraFragment. So I suspect this is the navigation library's behavior but I am not sure if this is an expected feature or a bug. 

--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/183e1ed5-29d3-4eb2-b3a2-4373b6a185b4n%40android.com.

Gerry Fan

unread,
Jan 10, 2022, 11:35:29 AM1/10/22
to Android CameraX Discussion Group, Scott Nien, Android CameraX Discussion Group, rahmice...@gmail.com
It can be sample issue: created a bug on the sample repo to track and investigate this one. 

Gerry Fan

unread,
Jan 10, 2022, 11:36:09 AM1/10/22
to Android CameraX Discussion Group, Gerry Fan, Scott Nien, Android CameraX Discussion Group, rahmice...@gmail.com
Reply all
Reply to author
Forward
0 new messages