title: Will the Observable stop emitting midway in this scenario due to being garbage collected?
body: <p>I was wondering if the observable is eligible to be garbage collected in this scenario:</p>
  
  <pre><code>fun getObservable() = Observable.interval(500, TimeUnit.Milliseconds)
  
  fun main(args: Array&lt;String&gt;) {
      getObservable().subscribe { println(it) }
  
      //Just to be able to observe the output.
      Thread.sleep(20*1000)
  }
  </code></pre>
  
  <p>It doesn't stop emitting. But I can't use it to be sure that it will not be GC'd after some time since it may just be that GC isn't triggering in this short span.</p>
  
  <p>My guess is that it should be GC'd. We don't have a reference to the <code>Observable</code> returned from the <code>getObservable()</code> function since we can't access it anymore. Calling the function again will give a different new <code>Observable</code>. Since we can't reach the <code>Observable</code> itself, the rest of the objects are also unreachable. So, I think all of this can be garbage collected at any random time.</p>
  
  <p>Some of my code in an Android App uses somewhat similar code. And it is an <code>Observable</code> that I hope will last as long as the <code>Application</code>. Since, GC will collect any garbage, I don't want my <code>Observable</code> to be GC'd in the middle of my app. So, please keep that in mind that I'm not looking for answers telling me whether it will be GC'd or not. What I care about is whether it can be GC'd.</p>
  
  <p>Thank you for taking out your time to help me.</p>
  
  <p>EDIT: To make the context clearer, here's the basic idea of the concerned code as I use it in my app.</p>
  
  <p>Repository.kt:</p>
  
  <pre><code>interface Repository {
  
      //Other code.
  
      fun getUserPrefs(): Flowable&lt;UserPreference&gt;
  }
  </code></pre>
  
  <p>MyViewModel.kt:</p>
  
  <pre><code>class MyViewModel(private val repository: Repository): ViewModel() {
  
      init {
          repository.getUserPrefs()
              .subscribe { //Code with UI state side effects }
      }
  }
  </code></pre>
  
  <p>In my app, Repository's implementation will be a singleton.For those not familiar with Android, just know that ViewModel's can last for pretty long time. It's work is basically to be responsible for managing UI state.</p>