Skip to main content

How does Angular handle memory management for components and subscriptions?

Senior Angular
Quick Answer Memory management in Angular: subscribe creates subscriptions that must be cleaned up. In ngOnDestroy: unsubscribe all Observables, use takeUntil(this.destroy$) pattern, clear intervals. Async pipe auto-unsubscribes when component destroys. Common leaks: subscriptions in services that outlive components, event listeners on window/document not removed, detached DOM nodes holding component references.

Answer

Angular destroys component instances automatically when navigating, but developers must prevent memory leaks by:

  • Unsubscribing Observables using async pipe or takeUntil.
  • Clearing intervals/timeouts.
  • Removing manual event listeners.
  • Avoiding storing component references in services.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice