Sub topic 15
Checking for memory leaks with the Memory panel
This chapter assumes you can open Chrome DevTools, record a trace, and write Angular components with lifecycle hooks. It covers how to confirm that an Angular app keeps memory it should have released, and how to find the cause.
What a leak looks like
A memory leak happens when objects that your app no longer needs stay reachable, so the garbage collector cannot free them. In Angular, the usual cause is something that outlives a component and still points at it. Common examples are a window event listener, an RxJS subscription, or a setInterval that nobody cleans up.
The symptom is memory that grows each time you repeat an action, such as opening and closing a dialog. It does not drop after garbage collection.
Step 1: Confirm growth
- Open the Memory panel and choose Heap snapshot.
- Click Take snapshot. This is your baseline.
- Repeat the suspect action five to ten times, for example navigating to a route and back.
- Click the trash can icon (Collect garbage) in the panel toolbar.
- Take a second snapshot.
Collecting garbage before the snapshot matters. Without it, you may see objects that are unreachable and about to be freed, which are not leaks.
Step 2: Compare snapshots
Select the second snapshot and change the view from Summary to Comparison, with the first snapshot as the base. Sort by # Delta. Look for classes whose count grows by roughly the number of times you repeated the action. If you opened a dialog 10 times and see 10 extra DialogComponent objects, that component is being retained.
You can also type Detached in the class filter. A detached DOM node is an element that was removed from the page but is still referenced by JavaScript. These often point to a component that was destroyed but not released.
Step 3: Find what holds the object
Click one of the leaked objects. The Retainers section below shows the chain of references that keeps it alive. Read it from the bottom up. You will often see something like an event listener, a closure, or a subscriber at the root of the chain. That is the code you need to fix.
Step 4: Fix and re-check
The scroll example from the earlier chapter on running events outside the zone already removes its listener in ngOnDestroy. Without that call, the listener would hold the component. Here is a leaking version and a fixed version that uses DestroyRef.
import { Component, DestroyRef, OnInit, inject } from '@angular/core';
import { interval } from 'rxjs';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
@Component({
selector: 'app-ticker',
template: `<p>Ticks: {{ ticks }}</p>`,
})
export class TickerComponent implements OnInit {
private destroyRef = inject(DestroyRef);
ticks = 0;
ngOnInit() {
// Leaking version: interval(1000).subscribe(() => this.ticks++);
interval(1000)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => this.ticks++);
}
}Repeat the same steps after the fix. The delta for TickerComponent should now be near zero.
Summary table
| Panel feature | Use it to |
|---|---|
| Collect garbage | Remove objects that are already unreachable |
| Comparison view | See which classes grew between snapshots |
| `Detached` filter | Find removed DOM nodes still referenced |
| Retainers | Trace the reference chain to the root cause |
Exercise
You open and close a dialog 8 times. The Comparison view shows +8 DialogComponent instances, even after you clicked Collect garbage. What does this tell you, and what do you check next?
Show answer
The dialog component is being retained after it is destroyed, which indicates a leak. Select one instance and read its Retainers chain to find what references it, such as a subscription, a timer, or an event listener. Then release it with takeUntilDestroyed, an unsubscribe, or removeEventListener in ngOnDestroy, and repeat the snapshots to confirm the delta drops to about 0.