Skip to main content

How does Angular handle memory leaks and what causes them?

Senior Angular
Quick Answer Angular memory leaks: Observables subscribed in components but never unsubscribed (subscription keeps component alive). Fix: use async pipe (auto-unsubscribes), takeUntilDestroyed() (Angular 16+), or takeUntil with destroy$ Subject. Event listeners on global objects (window, document) added in ngOnInit but not removed in ngOnDestroy. Detached component references in third-party library callbacks.

Answer

Common leak causes include:

  • Unsubscribed Observables
  • DOM references kept alive
  • Event listeners not removed
  • Intervals/timeouts not cleared

Angular avoids leaks via async pipe, takeUntil, DestroyRef, and proper cleanup patterns.

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