Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Does a coroutine prevent an object from being garbage collected?

Posted on November 14, 2022

Solution 1 :

True. Launching a long-running coroutine from everywhere will retain objects that it references until it’s finished or cancelled.

That’s why it’s advisable to use viewModelScope when inside ViewModel and lifecycleScope in activities and fragments. Because they know how to cancel those jobs.

https://developer.android.com/topic/libraries/architecture/coroutines

But if you don’t reference your activity or context (which is actually the activity itself) they won’t leak.


   CoroutineScope(SupervisorJob())
            .launch { 
                // activity.getString(R.string.something) // Memory leak!
                applicationContext.getString(R.string.something) // Memory leak on applicationContext but not on activity. That's fine.

            }
. . .
   }

Or at least that’s how I’d expect it to work. But there seems to be an unexpected behavior when you do withContext(NonCancellable) inside there. See https://github.com/Kotlin/kotlinx.coroutines/issues/1061.

In short, write it so there’s no references to Activity and other objects that reference Activity, as is good practice. Otherwise expect things to leak. I’d say the same if you reference any viewmodel objects inside a coroutine.

Problem :

I’ve seen tutorials where writing to Room performed in a coroutine launched using viewModelScope, so it’s cancelled when ViewModel is destroyed. But I want data to be written in any case. So I want to launch this coroutine in a scope that doesn’t depend on Activity’s lifecycle.

class MyApplication : Application() {
  val applicationScope = CoroutineScope(SupervisorJob())
}

Is it true that if I launch a long-running coroutine in ViewModel using this scope ViewModel won’t be allowed to be garbage collected until the coroutine is finished even if Activity is destroyed?

Comments

Comment posted by M.Ed

This is an excellent answer but I think it’s worth pointing out to people that there is no “fine” memory leaks, and that referencing the application’s context isn’t a memory leak itself, since the Application object would never be eligible for garbage collection since it’s constantly running and needed anyway, i.e. it’s OK to reference it in scopes with smaller lifecycles.

READ  [ANSWERED] android - Flutter: Right overflowed by 18 pixels
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress