Skip to content

Angular navigation transitions retain component references via canDeactivateChecks #63983

@brodziakm

Description

@brodziakm

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

The Angular router's navigation transition structure seems to retain component references after navigating, preventing garbage collection of the old route's component instance. This appears to happen for three navigation transitions.

It's not immediately apparent if there's a good reason for this, but we would hope that instances of old components could be garbage collected after they have transitioned out of the active route.

To reproduce:

  1. Open the small application at https://stackblitz.com/edit/stackblitz-starters-ewepd3bl?file=src%2Fmain.ts (open preview in a new tab and open developer tools to run memory profiling)
  2. Navigate to links One, Two, Three, and Four in sequence
  3. Capture a heap snapshot
  4. Look for 'Component' in the summary - this will include _ComponentB, _ComponentC, and _ComponentD, but notably not _ComponentA

The retainers appear to be due to how the canDeactivate guards work within the router, and it appears that these internal guards are holding a reference to the previous three component(s)

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-ewepd3bl?file=src%2Fmain.ts

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 20.3.0
Node: 20.19.1
Package Manager: npm 10.8.2
OS: linux x64
    

Angular: 20.3.0
... animations, build, cli, common, compiler, compiler-cli, core
... forms, platform-browser, router

Package                      Version
------------------------------------
@angular-devkit/architect    0.2003.0
@angular-devkit/core         20.3.0
@angular-devkit/schematics   20.3.0
@schematics/angular          20.3.0
rxjs                         7.8.2
typescript                   5.9.2
zone.js                      0.15.1

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions