I am having trouble understanding how memory management works on Android with locally created coroutines. Here is some code:
init {
CoroutineScope(Dispatchers.Default).launch {
val result = workerA.runSuspended(paramA)
liveDataA.postValue(result)
}
}
For a short suspend function call, I want to launch a coroutine once, then I no longer need the scope. I understand you cannot cancel the scope when written like this. But my main questions are:
- what happens once the job is done? Is the coroutineScope garbage collected?
- Is the scope guaranteed to stay alive until the job finishes?
- What happens if the runSuspended stays stuck? Will there be a memory leak if the parent object is garbage collected?