Skip to content

Seeing detached elements in a memory snapshot when I navigate from one route to another #58828

Closed as not planned

Description

@Tomas-CloudIQ

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

router

Is this a regression?

No

Description

I am using Angular router module and trying to fix a memory leak problem.
In a sample application, I have 2 routes and I am in Page1.
Once I click anywhere in Page1 component and go to Page2 via routerLink, I can see Page1 component as a detached element in memory snapshot.
This is a screenshot.
Image
I am in test-2 page now but can see app-test1 component is detached.

I tried in different Angular versions (12, 16, 19) and the behavior was exactly same.
Of course, I don't have any observables, pipes, services, ... My components are clean and it doesnt have any extra codes in component.ts files.
And I used only a and div tags in html.
It doesnt have any parent-child relationship too.

Can you please help me how I can fix this? or is this expected behavior in web applications?

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw


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

Angular: 19.0.0
Node: 18.19.1
NVM
OS: Windows 10
Package.json
{
  "name": "new-test-detached",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^19.0.0",
    "@angular/common": "^19.0.0",
    "@angular/compiler": "^19.0.0",
    "@angular/core": "^19.0.0",
    "@angular/forms": "^19.0.0",
    "@angular/platform-browser": "^19.0.0",
    "@angular/platform-browser-dynamic": "^19.0.0",
    "@angular/router": "^19.0.0",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.15.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^19.0.1",
    "@angular/cli": "^19.0.1",
    "@angular/compiler-cli": "^19.0.0",
    "@types/jasmine": "~5.1.0",
    "jasmine-core": "~5.4.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.2.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.1.0",
    "typescript": "~5.6.2"
  }
}

Anything else?

No response

Activity

  1. added this to the needsTriage milestone on Nov 22, 2024
  2. JoostK commented on Nov 22, 2024

    @JoostK
    Member

    Without runnable reproduction this won't be actionable to us.

  3. added
    needs reproduction
    on Nov 22, 2024
  4. modified the milestones: needsTriage, Backlog on Nov 22, 2024
  5. JoostK commented on Nov 25, 2024

    @JoostK
    Member

    Thanks, that does show a retainer from Angular DevTools:

    Image

    Heap-20241125T230037.heapsnapshot.zip

    With the extension disabled (e.g. in Incognito mode) I don't see the same retainer, nor is it very persistent: opening DevTools caused it to be dropped.

  6. JoostK commented on Nov 25, 2024

    @JoostK
    Member

    So the question is, are you experiencing the leak in production mode/without DevTools too?

  7. added
    memory leak
    and removed
    needs reproduction
    on Nov 25, 2024
  8. modified the milestones: Backlog, needsTriage on Nov 25, 2024
  9. 7 remaining items

  10. Tomas-CloudIQ commented on Nov 25, 2024

    @Tomas-CloudIQ
    Author

    @JeanMeche , @JoostK
    Can you please let me know how to take a snapshot while running in production mode?
    Is it same as we do in dev mode? (with chrome devtools)

  11. JeanMeche commented on Nov 26, 2024

    @JeanMeche
    Member

    Yes, it's the same. It's your app that needs to be build for production.

  12. Tomas-CloudIQ commented on Nov 27, 2024

    @Tomas-CloudIQ
    Author

    Thanks for your reply.
    @JoostK , I have one more questions from your reply.
    #58828 (comment)
    How can I find that retainer comes from dev tools?

  13. Tomas-CloudIQ commented on Nov 27, 2024

    @Tomas-CloudIQ
    Author

    Hi @JoostK , @JeanMeche
    We are facing same issue again when we use angular material.
    Currently, we are using matMenu and we can see some elements related to mantMenu are detached.
    Here is an example link.
    https://stackblitz.com/edit/v7o3ky-av3em4?file=package.json
    if I click some menu items and go to test2 route, I can see menu elements are detached.
    I followed the same steps you mentioned above. I did not open devtools while I am testing on the page.
    Image

    Image

    Could you please help me on this?
    or is this an open issue when we use angular material?

  14. JeanMeche commented on Nov 27, 2024

    @JeanMeche
    Member

    So, the source of the leak is actually our animation module.

    Image

    This pretty much reminds me of #54149, where matSort had similar leaks. It was addressed very recently by angular/components#30057.

    Maybe @crisbeto can give some thoughts on this.

  15. Tomas-CloudIQ commented on Nov 27, 2024

    @Tomas-CloudIQ
    Author

    @JeanMeche, does it mean we will see same behavior in the pages where we use animation module?
    And we are using angular 12 now in our project.
    Will this fix (angular/components#30057) work for angular 12?
    I wonder if it will work for latest Angular version.

  16. JeanMeche commented on Nov 27, 2024

    @JeanMeche
    Member

    v12 is outside of the support scope. Please have a look at our support policy.

  17. JoostK commented on Nov 28, 2024

    @JoostK
    Member

    The animations memory leak is mostly benign, see #47748 (comment).

  18. Tomas-CloudIQ commented on Nov 28, 2024

    @Tomas-CloudIQ
    Author

    @JeanMeche , was this fix released to Angular 17 latest versions now?

  19. JeanMeche commented on Nov 28, 2024

    @JeanMeche
    Member

    Those fixes don't land in LTS versions but only the active one (currently v19).

  20. Tomas-CloudIQ commented on Nov 28, 2024

    @Tomas-CloudIQ
    Author

    @JeanMeche, So it was released only to v19?
    And do you have any plans to introduce this fix to v17?

  21. JeanMeche commented on Dec 11, 2024

    @JeanMeche
    Member

    We don't do any backport fix beside security fixes.
    I think we can close this per above. The was no repro provided that exhibits a real leak.

  22. angular-automatic-lock-bot commented on Jan 11, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  23. locked and limited conversation to collaborators on Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: devtoolsmemory leakneeds reproduction

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions