Why do we need to unsubscribe from observable

0 votes
Can you explain to me with an example why we need to unsubscribe from observable?
4 days ago in Angular by Nidhi
• 10,860 points
20 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

We need to unsubscribe from observables to prevent memory leaks and unnecessary resource consumption. Observables in reactive programming (e.g., in RxJS or Angular) can continue emitting values as long as they are active. If a subscription is not explicitly unsubscribed, it can lead to:

Memory Leaks: The observable retains references to subscribers, preventing garbage collection.

Unwanted Side Effects: The subscription may continue executing logic (e.g., API calls, UI updates) even after the component or service using it is no longer in use.

Performance Issues: Accumulated subscriptions can degrade performance over time.

Example (Angular):

import { Component, OnDestroy } from '@angular/core';

import { Subscription, interval } from 'rxjs';

@Component({

  selector: 'app-example',

  template: `...`,

})

export class ExampleComponent implements OnDestroy {

  private subscription: Subscription;

  constructor() {

    this.subscription = interval(1000).subscribe(value => {

      console.log(value);

    });

  }

  ngOnDestroy() {

    this.subscription.unsubscribe(); // Unsubscribe to avoid memory leaks

  }

}

answered 4 days ago by Tanya

edited 3 days ago

Related Questions In Angular

0 votes
1 answer

How can we redirect to another page from existing page on clicking alert?

hii, It is really simple to redirect from ...READ MORE

answered Feb 6, 2020 in Angular by Niroj
• 82,840 points
4,442 views
0 votes
1 answer

How to get the latest value from Subject or Observable

Hello @kartik, A Subject or Observable doesn't have a current value. When ...READ MORE

answered Sep 8, 2020 in Angular by Niroj
• 82,840 points
18,010 views
0 votes
1 answer

How can we redirect to an existing route using ngRoute?

Routing is just another way of fixing some content ...READ MORE

answered Feb 6, 2020 in Angular by Niroj
• 82,840 points
3,934 views
0 votes
1 answer

How can we go back to previous page after having some error on request made through current page ?

$route is used for deep-linking URLs to controllers ...READ MORE

answered Feb 11, 2020 in Angular by Niroj
• 82,840 points
1,433 views
0 votes
1 answer

How do we work with UI-Router in AngularJS?

Hello @kartik,  As you know Ui-Router is more ...READ MORE

answered Feb 11, 2020 in Angular by Niroj
• 82,840 points
931 views
0 votes
1 answer

How to pass a string parameter from angular UI to node.js backend?

Hello Kartik, There are three ways to get ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
11,080 views
0 votes
1 answer

How can I remove a port from url for node app using nginx

If you run your node server on ...READ MORE

answered Apr 10, 2018 in DevOps on Cloud by ajs3033
• 7,300 points
4,244 views
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
2,046 views
0 votes
1 answer

How to pass props to {this.props.children}?

Hello @kartik,  Try using this: <div> ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
3,587 views
0 votes
1 answer

How to specify a port to run a create-react-app based project?

Hello @kartik, You could use cross-env to set the port, ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
6,198 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP