Skip to content

Memory leak when using ngTemplateOutlet #15578

Closed

Description

@tsvetomir

I'm submitting a ...

[x] bug report

Current behavior
I'm using ngTemplateOutlet in a similar way to render a table:

<template #cell1 let-dataItem>
  {{dataItem.field1}}
</template>

<table>
  <tr *ngFor="let dataItem of data">
    <td>
      <template [ngTemplateOutlet]="cell1" [ngOutletContext]="{ $implicit: dataItem }">
      </template>
    </td>
  </tr>
</table>

The heap size and node count grows uncontrollably when updating the data.

image

Expected behavior
The heap size and node count should remain fairly constant since we're only replacing their content.
Note that the application is running in production mode.

Minimal reproduction of the problem with instructions

  1. Open this plunkr for 2.0.10 or this plunkr for 4.0.0.
  2. Start your browsers memory profiler or observe the process memory usage
  3. Notice that memory is not reclaimed

What is the motivation / use case for changing the behavior?
Application memory usage should be stable.

Please tell us about your environment:
Linux

  • Angular version: 2.4.10, 4.0.0

  • Browser: Chrome 57 | Firefox 52

  • Language: TypeScript 2.0

Activity

  1. DaveMBush commented on Mar 29, 2017

    @DaveMBush

    For the record, this happen in Windows (7) environment as well

  2. alexzuza commented on Mar 29, 2017

    @alexzuza
    Contributor

    Will this solve the problem?

    ngOnDestroy() {
      if (this._viewRef) {
         this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._viewRef));
      }
    }
    

    Without ngOnDestroy
    2

    After applying the code above
    1

    https://plnkr.co/edit/ciBnuI1m6xYO9cyjj5zH?p=preview

  3. ericmartinezr commented on Mar 29, 2017

    @ericmartinezr
    Contributor

    related to #14919 ?

  4. tsvetomir commented on Mar 29, 2017

    @tsvetomir
    Author

    @alexzuza it helps indeed :) Your version of ngTemplateOutlet does not leak memory:

    image

    @ericmartinezr maybe the references are kept by the template outlet and selecting them with @ContentChildren is not necessary for the leak to occur.

  5. tbosch commented on Mar 29, 2017

    @tbosch
    Contributor

    Hi,
    could you make a version that also destroys the component that contains the declaration place of the <template>?

  6. tsvetomir commented on Mar 30, 2017

    @tsvetomir
    Author

    @tbosch I'm not sure what I should do. Should I move the templates to a sub-component?

  7. added
    area: core
    area: common
    and removed
    area: core
    on Apr 10, 2017
  8. tbosch commented on Apr 10, 2017

    @tbosch
    Contributor

    Yes, fixing ngTemplateOutlet is the right place.

  9. added
    memory leak
    and removed on Apr 10, 2017
  10. 1 remaining item

  11. tbosch commented on May 5, 2017

    @tbosch
    Contributor

    Thinking about this a bit more, I think we can fix this in a general way, without needing to change ngTemplateOutlet... Working on it...

  12. added a commit that references this issue on May 5, 2017
    c20121e
  13. added a commit that references this issue on May 8, 2017
    1b32dd7
  14. added a commit that references this issue on May 8, 2017
    f0f6544
  15. added a commit that references this issue on May 10, 2017
    ee6705a
  16. sgrinich commented on Jun 16, 2017

    @sgrinich

    Hi,

    I'm wondering what the minimum version of Angular 2.x is needed to resolve this bug. Our application is using Angular 2.4.10 and I am hoping to fix this memory leak without needing to upgrade the app to Angular 4.

  17. tsvetomir commented on Jun 18, 2017

    @tsvetomir
    Author

    @sgrinich upgrading from 2 to 4 shouldn't be too hard. Also 4.x is an LTS release.
    Check out the Angular Update Guide for a summary of the necessary changes.

  18. sgrinich commented on Jun 19, 2017

    @sgrinich

    Ok, thanks @tsvetomir

  19. added a commit that references this issue on Aug 11, 2017
    bf1b202
  20. added a commit that references this issue on Aug 28, 2017
    cddbaa6
  21. angular-automatic-lock-bot commented on Sep 11, 2019

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

  22. locked and limited conversation to collaborators on Sep 11, 2019
  23. added a commit that references this issue on Nov 20, 2026
    721a9b7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: commonfreq2: mediummemory leaktype: bug/fix

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions