Angular interview question:
What is one of the easiest ways to create a memory leak in an Angular app?
A very common answer is:
“Forgetting to unsubscribe.”
That is correct, but the real issue is usually deeper.
The problem is not just the missing unsubscribe().
The problem is uncontrolled subscriptions.
For example:
ngOnInit() { this.userService.getUser().subscribe(user => { this.user = user; }); }
This may look harmless.
But if the observable does not complete automatically, and the component gets destroyed, the subscription can stay alive.
Now imagine this pattern repeated across:
dashboards
filters
search inputs
WebSocket streams
route-based components
real-time notifications
Small leaks become real production problems.
A better approach is to avoid manual subscriptions when possible.
Use the async pipe:
<div *ngIf="user$ | async as user"> {{ user.name }} </div>
Or in newer Angular versions, use takeUntilDestroyed():
this.userService.getUser() .pipe(takeUntilDestroyed(this.destroyRef)) .subscribe(user => { this.user = user; });
My rule of thumb:
If the data is only used in the template, prefer async pipe.
If you need side effects in TypeScript, make the subscription lifecycle explicit.
Senior Angular development is not just knowing RxJS operators.
It is knowing who owns the subscription — and when it dies.
How do you usually manage subscriptions in Angular?
#Angular #RxJS #FrontendDevelopment #TypeScript #WebDevelopment #SoftwareEngineering #InterviewQuestions
