Skip to content

Detached elements stay in memory when animated #50533

Closed

Description

@waterplea

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

animations

Is this a regression?

No

Description

If you use an animation on the element and toggle it with ngIf, it seems to be staying in the memory according to Detached Elements debugger in Edge. On a brief look it appears that maybe a WeakMap has to be used some place instead of Map:

image

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-vnp1kv

  1. Open this link in Edge browser
  2. Open DevTools -> Detached Elements tab
  3. Select the correct frame in the dropdown on that tab (it should say https://stackblitz-starters-vnp1kv.stackblitz.io/)
  4. Toggle element a few times with the button
  5. Press get detached element
  6. Press analyze to see what keeps the elements there (you will see the above screenshot)

Please provide the exception or error you saw

Multiple copies of DOM element stay in memory.

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

I noticed it in Angular 12, but it is reproducible in 16 as well, see linked StackBlitz

Anything else?

An article I learned about Detached Elements from:
https://blogs.windows.com/msedgedev/2021/12/09/debug-memory-leaks-detached-elements-tool-devtools/

Activity

  1. added
    area: animations
    on May 31, 2023
  2. added this to the needsTriage milestone on May 31, 2023
  3. garrettld commented on May 31, 2023

    @garrettld
    Contributor

    The StackBlitz doesn't reproduce the issue for me, this is all I see after toggling the element and clicking 'Get Detached Elements':
    image

  4. waterplea commented on May 31, 2023

    @waterplea
    ContributorAuthor

    The StackBlitz doesn't reproduce the issue for me, this is all I see after toggling the element and clicking 'Get Detached Elements': image

    Yes, now click the ID and it will expand the element behind it. Also, forgot to make sure, since StackBlitz is in an iframe you need to select the right window:

    image

  5. garrettld commented on Jun 1, 2023

    @garrettld
    Contributor

    Thanks for your help on the tooling! I'm sorry I don't have a resolution for you but I did notice a couple things that might help someone debugging this:

    1. The detached HTMLDivElements still show in the dev tools even if you remove the @HostBinding() that applies the animation to the host element.
    2. The detached HTMLDivElements disappear if you define the animation in the parent component instead and apply it to the child like <div element @fade>.

    This seems to indicate that the animation definition (animations: [...]) is causing its host element to be retained, as opposed to the animation usage (@fade).

  6. JeanMeche commented on Jun 3, 2023

    @JeanMeche
    Member

    Isn't this a dupe of #20585 ?

  7. waterplea commented on Jun 3, 2023

    @waterplea
    ContributorAuthor

    Isn't this a dupe of #20585 ?

    Well, not necessarily, although the underlying issue might be the same. The linked issue talks about manually creating animations with builder and player and so on. Here I have the most basic declarative :enter/:leave animation in metadata with 0 fiddling with the stuff that's under the hood.

  8. JeanMeche commented on Jul 3, 2023

    @JeanMeche
    Member

    Looks like the issue is the hostbinding like here #24197

    Having a noop animation on the parent fixes the leak. (animations: [trigger("noop", [])] )

    Also the leak appears only where there is a :leave animation.

  9. JeanMeche commented on Jul 3, 2023

    @JeanMeche
    Member

    I've proposed a fix.

  10. added
    memory leak
    on Jul 3, 2023
  11. JeanMeche commented on Jul 11, 2023

    @JeanMeche
    Member

    #50929 has been merged !

  12. angular-automatic-lock-bot commented on Aug 11, 2023

    @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.

  13. locked and limited conversation to collaborators on Aug 11, 2023
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: animationsmemory leakstate: has PR

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions