What if your app secretly wastes memory and slows down without you knowing?
Why Unsubscribing and memory leaks in Angular? - Purpose & Use Cases
Imagine you build an Angular app that listens to many data streams, like user clicks or server updates, but you never stop listening even when parts of the app are gone.
Without stopping these listeners, your app keeps using memory and CPU for things no longer needed. This slows down the app and can even crash the browser.
Unsubscribing from streams when they are no longer needed frees up resources automatically, keeping your app fast and healthy.
this.subscription = observable.subscribe(data => { /* handle data */ }); // never unsubscribedthis.subscription = observable.subscribe(data => { /* handle data */ });
ngOnDestroy() {
this.subscription.unsubscribe();
}It enables your Angular app to run smoothly without wasting memory or slowing down over time.
Think of it like turning off the lights when you leave a room; unsubscribing turns off data streams when components are gone.
Not unsubscribing causes memory leaks and slow apps.
Unsubscribing cleans up resources automatically.
This keeps Angular apps fast and reliable.
Practice
Solution
Step 1: Understand Observable subscriptions
Subscriptions keep listening to data streams, even if the component is gone.Step 2: Recognize the effect of not unsubscribing
Not unsubscribing causes memory to be used unnecessarily, leading to leaks.Final Answer:
To prevent memory leaks by stopping ongoing subscriptions when the component is destroyed. -> Option BQuick Check:
Unsubscribe to avoid memory leaks = B [OK]
- Thinking Angular unsubscribes automatically
- Believing unsubscribing improves load speed
- Ignoring memory leaks from active subscriptions
ngOnDestroy method?Solution
Step 1: Recall Subscription API
The Subscription object has anunsubscribe()method to stop listening.Step 2: Identify correct method name
Onlyunsubscribe()is valid; others do not exist on Subscription.Final Answer:
this.subscription.unsubscribe(); -> Option AQuick Check:
Use unsubscribe() to stop subscription = D [OK]
- Using non-existent methods like stop() or cancel()
- Forgetting to call unsubscribe() at all
- Calling unsubscribe() outside ngOnDestroy
export class MyComponent implements OnDestroy {
private sub = this.service.getData().subscribe(data => console.log(data));
ngOnDestroy() {
// missing unsubscribe
}
}What will happen if this component is created and destroyed multiple times without adding unsubscribe in
ngOnDestroy?Solution
Step 1: Analyze missing unsubscribe
Without calling unsubscribe, the subscription stays active even after component destruction.Step 2: Understand consequences
Repeated creation and destruction cause multiple active subscriptions, increasing memory use.Final Answer:
Memory usage will increase over time due to active subscriptions not being cleaned up. -> Option AQuick Check:
Missing unsubscribe causes memory leaks = A [OK]
- Assuming Angular auto-unsubscribes
- Expecting errors on destroy without unsubscribe
- Thinking subscription stops automatically
export class DemoComponent implements OnDestroy {
private subscription: Subscription;
ngOnInit() {
this.subscription = this.service.getData().subscribe();
}
ngOnDestroy() {
this.subscription.unsubscribe;
}
}Solution
Step 1: Check unsubscribe usage
The code usesthis.subscription.unsubscribe;without parentheses, so the method is not executed.Step 2: Understand effect
Without calling unsubscribe(), the subscription remains active, causing memory leaks.Final Answer:
The unsubscribe method is not called; it is referenced without parentheses. -> Option DQuick Check:
Call unsubscribe() with () to execute it = A [OK]
- Forgetting parentheses on unsubscribe
- Assuming referencing method unsubscribes
- Not assigning subscription before unsubscribe
Solution
Step 1: Understand managing multiple subscriptions
Using a Subscription object lets you add multiple subscriptions and unsubscribe all at once.Step 2: Compare with manual unsubscribing
Manual unsubscribing is error-prone and verbose; Subscription.add() simplifies cleanup.Final Answer:
Store all subscriptions in a Subscription object and call unsubscribe() once in ngOnDestroy. -> Option CQuick Check:
Use Subscription.add() and one unsubscribe() call = C [OK]
- Forgetting to unsubscribe all subscriptions
- Relying on Angular to auto-unsubscribe
- Using setTimeout which delays cleanup unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
