Bird
0
0

Consider this Angular component snippet:

medium📝 component behavior Q13 of 15
Angular - RxJS and Observables Fundamentals
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?
AMemory usage will increase over time due to active subscriptions not being cleaned up.
BThe component will automatically unsubscribe, so no issues occur.
CThe subscription will throw an error immediately on component destroy.
DThe component will reload data only once and then stop.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze missing unsubscribe

    Without calling unsubscribe, the subscription stays active even after component destruction.
  2. Step 2: Understand consequences

    Repeated creation and destruction cause multiple active subscriptions, increasing memory use.
  3. Final Answer:

    Memory usage will increase over time due to active subscriptions not being cleaned up. -> Option A
  4. Quick Check:

    Missing unsubscribe causes memory leaks = A [OK]
Quick Trick: No unsubscribe means memory leaks grow with component reuse [OK]
Common Mistakes:
MISTAKES
  • Assuming Angular auto-unsubscribes
  • Expecting errors on destroy without unsubscribe
  • Thinking subscription stops automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes