question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CameraX memory leak

See original GitHub issue

Download CameraX sample. Add new empty activity. Add LeakCanary dependency and LeakSentry.refWatcher.watch(this) to onDestroyView of CameraFragment. After app started and all permissions granted start new activity and call finish on MainActivity. Like this f.x.

        Handler().postDelayed({
            startActivity(Intent(this, TestActivity::class.java))
            finish()
        }, 10000L)

You will see CameraX instance is leaking. Some screens from my app. In your sample leaks are the same.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Boucaacommented, Mar 2, 2020

I am facing the same issue, but @ocarey 's workaround sometimes crashes the app. I’ve also noticed that for every instance of the activity that’s leaked, there is also a leaked androidx.lifecycle.ReportFragment, so I’m guessing that it’s a bug in the camera lifecycle library (androidx.camera:camera-lifecycle)

3reactions
ocareycommented, Feb 6, 2020

By adding this to Activity onDestroy() I was able to force a Garbage Collection of my Camera Activity. Version alpha09 w/ CameraXBasic example app:

override fun onDestroy() {
        super.onDestroy()
        tearDownCamera()
    }
private fun tearDownCamera() {
        val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
        cameraProviderFuture.addListener(Runnable {
            val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
            cameraProvider.shutdown()
        }, ContextCompat.getMainExecutor(this))
    }

Best, Owen

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory leak on official CameraX sample - Google Groups
I was using CameraX on our current project and getting some ridiculous crashes at fragments that implements camera preview use case, ...
Read more >
Memory leak on official CameraX sample? : r/androiddev
I was using CameraX on our current project and getting some ridiculous crashes at fragments that implements camera preview use case, ...
Read more >
Memory leak on CameraX preview use case - Stack Overflow
I was using CameraX on our current project and getting some ridiculous crashes at fragments that implements camera preview use case, ...
Read more >
Memory leak from PreviewView [240077164] - Issue Tracker
According to the leaking stack information, it seams like that your app has a CameraEnhancer class that will hold the Preview use case...
Read more >
CameraX | Android Developers
CameraX is an addition to Jetpack that makes it easier to add camera ... Fixed a memory leak caused by multiple threads concurrently...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found