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 garbag
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
Observable
doesn't automatically "free" anything, but give you the tools to do it.
there are some aspects of resource management with Observable:
Observable
gives you, it's the Cancellable
/Disposable
components, the framework guarantees that this events will be triggered at the appropriate time - when Observable completes, when unsubscribing/disposing Observable
. 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 logicSubscriber
from the Obesrvable
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. while Observable
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 the Observable
.Observable
and the Subscriber
, for preventing memory leaks, as the Subscriber
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 nature Observable
)