Local CoroutineScope().launch { }, how long does the created coroutinescope live in memory?

Viewed 56

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:

  1. what happens once the job is done? Is the coroutineScope garbage collected?
  2. Is the scope guaranteed to stay alive until the job finishes?
  3. What happens if the runSuspended stays stuck? Will there be a memory leak if the parent object is garbage collected?
0 Answers
Related