Possible memory leak when GoogleMap component is disposed but Activity is not destroyed
See original GitHub issueIt seems like there could be a possible memory leak when the GoogleMap is disposed without the activity being destroyed. When it is disposed, the lifecycle observer is removed and onDestroy
seems to not be called on the map. Does that seem accurate or is onDestroy
being called somewhere else as well?
Steps to reproduce:
- Modify the
onCreate
method in MapSampleActivity to conditionally add the map to the composition a. See sample code below - launch the sample application
- optionally place a breakpoint in the
onDispose
block ofMapLifecycle
inGoogleMap.kt
- optionally place a breakpoint at the start of the
when
inside thelifecycleObserver
extension function inGoogleMap.kt
- optionally Attach the debugger
- press the βHide mapβ button at the bottom of the screen
- Notice that onDispose is called but
onDestroy
was not called for the MapView since the activity is still alive - Backing out of the app at this point destroys the activity but does not result in any calls to
onDestroy
for the MapView as far as I can tell
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var isMapLoaded by remember { mutableStateOf(false) }
var shouldShowMap by remember { mutableStateOf(true) }
Box(Modifier.fillMaxSize()) {
if (shouldShowMap) {
GoogleMapView(
modifier = Modifier.matchParentSize(),
onMapLoaded = {
isMapLoaded = true
}
)
}
if (!isMapLoaded) {
AnimatedVisibility(
modifier = Modifier
.matchParentSize(),
visible = !isMapLoaded,
enter = EnterTransition.None,
exit = fadeOut()
) {
CircularProgressIndicator(
modifier = Modifier
.background(MaterialTheme.colors.background)
.wrapContentSize()
)
}
}
Button(
onClick = { shouldShowMap = false },
modifier = Modifier.align(Alignment.BottomCenter)
.padding(bottom = 16.dp)
) {
Text(text = "Hide map")
}
}
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Memory leak #138 - googlemaps/android-maps-compose
I am getting a leak if the map component is cleared and then the activity is destroyed. ... The reason is that Lifecycle.Event.ON_DESTROY...
Read more >Google Map View is leaking very much
Huge memory leaking. I've 'destroyed' the MapView like I should when exiting the activity and it still leaks. The MapView is in an...
Read more >How to get rid of memory leaks? A practical approach using ...
LeakCanary hooks into the Android lifecycle and detects when activities and fragments are destroyed and should be garbage collected. Then these objects areΒ ......
Read more >Bug: OutOfMemory after update to Play Services 6.1.09 ...
We are getting the same issue. Basically we have an activity with a Mapfragment. Even when the activity is destroyed, MapView seems to...
Read more >Everything you need to know about Memory Leaks in ...
A memory leak can easily occur in Android when AsyncTasks, Handlers, Singletons, Threads, and other components are used incorrectly. I'll use threads,Β ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have confirmed that is the case for the sample app. I am seeing a memory leak in my app but I will need to do more digging to see what the cause of that is. My current theory is that it has something to do without housing the map component inside an AbstractComposeView, but Iβll know more when I have a chance to dig into it more
@arriolac 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 ma[ps leak, I had to use:
And
OnDestroy might not be called and this way you would always dispose of the view in onDispose, which will always be called. Would you be able to add this?
`1 APPLICATION LEAKS