[angular 2] Is it necessary to dispose of Observable subscriptions in ngOnDestroy()?

346 views
Skip to first unread message

Brian Kotek

unread,
Apr 15, 2016, 12:07:41 PM4/15/16
to AngularJS
One thing that I'm not clear on is whether I'm required to manually dispose Observable subscriptions I set up in a component.

For example say I do this in ngOnInit():
this.someService.somethingChanged.subscribe( ( value ) => this.onSomethingChanged( value ) );

Am I required to store that subscription and manually dispose() of it in ngOnDestroy()? And if I fail to do this, am I introducing a memory leak?

Thanks,

Brian

Günter Zöchbauer

unread,
Apr 16, 2016, 12:53:17 PM4/16/16
to AngularJS
When the components class isn't referenced anywher anymore it will be garbage collected and your subscription with it (when no other class holds a reference to it)
Reply all
Reply to author
Forward
0 new messages