问题
Do I need to free/release Observable object after I use it in case Observable object is not garbaged by GC? I didn't find any article to mention it. Is Observable object garbaged by GC automatically?
Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(ObservableEmitter<Boolean> e) throws Exception {
boolean connected = true;
//my logic code here
e.onNext(connected);
e.onComplete();
e.setCancellable(null);
//Do I need to do some extra release/free Observable object work in case Observable object is not garbaged by GC?
}
}).subscribeOn(Scheduler.ioThread())
.observeOn(Scheduler.mainThread())
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean connected) throws Exception {
//my logic code here.
//Do I need to do some extra release/free Observable object work in case Observable object is not garbaged by GC?
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
//my logic code here.
//Do I need to do some extra release/free Observable object work in case Observable object is not garbaged by GC?
}
});
回答1:
In most cases we will not need to explicitly call the unsubscribe method unless we want to cancel early or our Observable has a longer lifespan than our subscription. The default behavior of Observable operators is to dispose of the subscription as soon as .complete() or .error() messages are published. Keep in mind that RxJS was designed to be used in a "fire and forget" fashion most of the time. read
回答2:
Observable
doesn't automatically "free" anything, but give you the tools to do it.
there are some aspects of resource management with Observable:
- Releasing any resource associated with the 'Producer' - the resource that generates events, this resource can be anything from IO related resource - network query, some hardware/sensor related events, DB triggers; UI related resource - clicks, scrolling etc.
That's needs to manage by you with the framework thatObservable
gives you, it's theCancellable
/Disposable
components, the framework guarantees that this events will be triggered at the appropriate time - when Observable completes, when unsubscribing/disposingObservable
. but it's up to you to trigger the appropriate logic - cancelling the network query, shutdown/release/close IO resource, unregister from your Listener etc, all depends on your Producer logic - Disposing/unsubscribing the
Subscriber
from theObesrvable
explicitly when your resource is no longer needed - while the Observable gives you the framework to clear resources, it's up to you to trigger it at explicit times. whileObservable
will trigger it automatically when Observable completes, that sometimes too late and you want to cancel it before (i.e. network query ) and with other cases your Observable might be of infinite items, think of DB inserts listener, clicks events or any other listener, with this cases the resource will be held up until you will explicitly release it by disposing theObservable
. - It is also important to untie (dispose) the connection between the
Observable
and theSubscriber
, for preventing memory leaks, as theSubscriber
might hold hard reference to wrapping classes and thus will be leaked by the Observable. (classic with UI components, where subscriber reference some UI class and subscribe to some static natureObservable
) - It worth noting that disposing Observable will trigger Thread interrupt to the Producer subscription thread. so in case you have IO operation there, you might get InterruptedException.
来源:https://stackoverflow.com/questions/44855841/do-i-need-to-free-release-observable-object-after-i-use-it