A memory leak happens when the app keeps using memory for things it no longer needs, like leftover subscriptions. This can slow down or crash the app over time.
Click to reveal answer
beginner
Why should you unsubscribe from Observables in Angular?
Unsubscribing stops the app from listening to data streams that are no longer needed. This frees memory and prevents memory leaks.
Click to reveal answer
beginner
Name a common Angular lifecycle hook used to unsubscribe from Observables.
The ngOnDestroy lifecycle hook is used to clean up subscriptions before the component is destroyed.
Click to reveal answer
intermediate
What is the purpose of the 'takeUntil' operator in Angular RxJS?
The 'takeUntil' operator automatically unsubscribes from an Observable when another Observable emits a value, often used with a 'destroy' notifier to clean up subscriptions.
Click to reveal answer
intermediate
How can using the async pipe in Angular templates help prevent memory leaks?
The async pipe automatically subscribes and unsubscribes from Observables when the component loads and unloads, so you don't have to manage subscriptions manually.
Click to reveal answer
What happens if you don't unsubscribe from an Observable in Angular?
AThe Observable stops emitting data.
BThe app will automatically unsubscribe for you.
CMemory leaks can occur because subscriptions stay active.
DNothing, it's safe to ignore.
✗ Incorrect
If you don't unsubscribe, the subscription stays active and can cause memory leaks.
Which Angular lifecycle hook is best for unsubscribing from Observables?
AngAfterViewInit
BngOnDestroy
CngOnInit
DngDoCheck
✗ Incorrect
ngOnDestroy is called just before the component is removed, making it ideal for cleanup like unsubscribing.
What does the 'takeUntil' operator do in RxJS?
AUnsubscribes when another Observable emits
BConverts Observable to Promise
CDelays the Observable emissions
DStarts a new Observable
✗ Incorrect
'takeUntil' unsubscribes from the source Observable when the notifier Observable emits.
How does the async pipe help with memory management?
AIt automatically unsubscribes when the component is destroyed
BIt caches data permanently
CIt prevents Observables from emitting
DIt delays subscription until user clicks
✗ Incorrect
The async pipe handles subscription and unsubscription automatically, preventing leaks.
Which of these is NOT a good practice to avoid memory leaks in Angular?
AUnsubscribe manually in ngOnDestroy
BUse takeUntil with a destroy notifier
CUse async pipe in templates
DKeep subscriptions active forever
✗ Incorrect
Keeping subscriptions active forever causes memory leaks and should be avoided.
Explain why unsubscribing from Observables is important in Angular and how you can do it.
Think about what happens when components are removed and how to clean up.
You got /4 concepts.
Describe three methods to prevent memory leaks caused by subscriptions in Angular.
Consider lifecycle hooks, template tools, and RxJS operators.
You got /3 concepts.
Practice
(1/5)
1. Why is it important to unsubscribe from Observables in Angular components?
easy
A. To make the component load faster initially.
B. To prevent memory leaks by stopping ongoing subscriptions when the component is destroyed.
C. To increase the number of active subscriptions.
D. To automatically refresh the component view.
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 B
Quick Check:
Unsubscribe to avoid memory leaks = B [OK]
Hint: Unsubscribe in ngOnDestroy to avoid leaks [OK]
Common Mistakes:
Thinking Angular unsubscribes automatically
Believing unsubscribing improves load speed
Ignoring memory leaks from active subscriptions
2. Which of the following is the correct way to unsubscribe from a subscription in Angular's ngOnDestroy method?
easy
A. this.subscription.unsubscribe();
B. this.subscription.stop();
C. this.subscription.close();
D. this.subscription.cancel();
Solution
Step 1: Recall Subscription API
The Subscription object has an unsubscribe() method to stop listening.
Step 2: Identify correct method name
Only unsubscribe() is valid; others do not exist on Subscription.
Final Answer:
this.subscription.unsubscribe(); -> Option A
Quick Check:
Use unsubscribe() to stop subscription = D [OK]
Hint: Use unsubscribe() method to stop subscriptions [OK]
Common Mistakes:
Using non-existent methods like stop() or cancel()
Forgetting to call unsubscribe() at all
Calling unsubscribe() outside ngOnDestroy
3. Consider this Angular component snippet:
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?
medium
A. Memory usage will increase over time due to active subscriptions not being cleaned up.
B. The component will automatically unsubscribe, so no issues occur.
C. The subscription will throw an error immediately on component destroy.
D. The component will reload data only once and then stop.
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 A
Quick Check:
Missing unsubscribe causes memory leaks = A [OK]
Hint: No unsubscribe means memory leaks grow with component reuse [OK]
Common Mistakes:
Assuming Angular auto-unsubscribes
Expecting errors on destroy without unsubscribe
Thinking subscription stops automatically
4. Identify the problem in this Angular component code related to unsubscribing: