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.

I have mentioned this in https://github.com/googlemaps/android-maps-compose/issues/26 , but it is already closed, so I am opening a new issue. I am getting a leak if the map component is cleared and then the activity is destroyed.

Previously when implementing it through AndroidView, to get rid of the maps leak, I had to use:

DisposableEffect(lifecycle) {
        lifecycle.addObserver(lifecycleObserver)
        onDispose {
            lifecycle.removeObserver(lifecycleObserver)
            mapView.onDestroy()
            mapView.removeAllViews()
        }
    }

And

remember(mapView) {
        LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_CREATE -> mapView.onCreate(Bundle())
                Lifecycle.Event.ON_START -> mapView.onStart()
                Lifecycle.Event.ON_RESUME -> mapView.onResume()
                Lifecycle.Event.ON_PAUSE -> mapView.onPause()
                Lifecycle.Event.ON_STOP -> mapView.onStop()
                Lifecycle.Event.ON_DESTROY -> {
                    //handled in onDispose
                }
                else -> throw IllegalStateException()
            }
        }
    }

The reason is that Lifecycle.Event.ON_DESTROY might not be called, and this way you would always dispose of the map view in onDispose, which will always be called. Would you be able to add this? I did fork and update it and the big memory leak disappeared.

`1 APPLICATION LEAKS

References underlined with "~~~" are likely causes.
Learn more at https://squ.re/leaks.

56446 bytes retained by leaking objects
Signature: 34a030bd011ab1ccd655bbeac3fbc8465c53ce5f
┬───
β”‚ GC Root: Thread object
β”‚
β”œβ”€ com.google.maps.api.android.lib6.gmm6.vector.n instance
β”‚    Leaking: UNKNOWN
β”‚    Retaining 3.5 MB in 26933 objects
β”‚    Thread name: 'RenderDrive'
β”‚    ↓ n.e
β”‚        ~
β”œβ”€ com.google.maps.api.android.lib6.gmm6.vector.p instance
β”‚    Leaking: UNKNOWN
β”‚    Retaining 3.5 MB in 26930 objects
β”‚    ↓ p.f
β”‚        ~
β”œβ”€ com.google.maps.api.android.lib6.gmm6.api.ac instance
β”‚    Leaking: UNKNOWN
β”‚    Retaining 3.5 MB in 26929 objects
β”‚    View not part of a window view hierarchy
β”‚    View.mAttachInfo is null (view detached)
β”‚    View.mWindowAttachCount = 1
β”‚    mContext instance of ExampleApplication
β”‚    ↓ ac.N
β”‚         ~
β”œβ”€ com.google.maps.api.android.lib6.impl.ax instance
β”‚    Leaking: UNKNOWN
β”‚    Retaining 20 B in 1 objects
β”‚    a instance of ExampleApplication
β”‚    b instance of ExampleActivity with mDestroyed = true
β”‚    ↓ ax.b
β”‚         ~
β•°β†’ ExampleActivity instance`

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

3reactions
polivmi1commented, Jun 26, 2022

@arriolac I have added a sample here https://github.com/polivmi1/android-maps-compose It contains 2 screens using compose navigation. Steps to reproduce the problem:

  1. Open in profiler and memory
  2. open basic map
  3. click on marker
  4. click on BACK button
  5. repeat steps 3 and 4 multiple times seeing that memory is never cleaned
  6. minimize the app to free the leaked objects

I am also attaching screenshots of the step 5 and 6: Screenshot 2022-06-26 at 17 33 45 Screenshot 2022-06-26 at 17 34 19

Please let me know if you need more information. It should be clear from this, but you can also add the lifecycle logs and try with the fix I mentioned in the first comment.

Thanks for looking into it!

2reactions
arriolaccommented, Aug 11, 2022

Ok I’ve confirmed that it is the intended behavior that the previous destination is not getting the DESTROYED event when navigating away from it. Your proposed workaround https://github.com/googlemaps/android-maps-compose/issues/138#issue-1263112472 should be put in place to resolve this @polivmi1. Would you like to make this contribution to the library?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory leak - Wikipedia
In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in...
Read more >
What is Memory Leak? How can we avoid? - GeeksforGeeks
Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it...
Read more >
Definition of memory leak - PCMag
When memory is allocated, but not deallocated, a memory leak occurs (the memory has leaked out of the computer). If too many memory...
Read more >
Memory leak - OWASP Foundation
A memory leak is an unintentional form of memory consumption whereby the developer fails to free an allocated block of memory when no...
Read more >
Find a memory leak - Windows drivers - Microsoft Learn
A memory leak occurs when a process allocates memory from the paged or nonpaged pools, but doesn't free the memory.
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 Reddit Thread

No results found

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