Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak when using ngTemplateOutlet #15578

Closed
tsvetomir opened this issue Mar 29, 2017 · 12 comments
Closed

Memory leak when using ngTemplateOutlet #15578

tsvetomir opened this issue Mar 29, 2017 · 12 comments
Assignees
Labels
area: common Issues related to APIs in the @angular/common package freq2: medium memory leak Issue related to a memory leak type: bug/fix

Comments

@tsvetomir
Copy link

tsvetomir commented Mar 29, 2017

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

@DaveMBush
Copy link

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

@alexzuza
Copy link
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

@ericmartinezr
Copy link
Contributor

related to #14919 ?

@tsvetomir
Copy link
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.

@tbosch
Copy link
Contributor

tbosch commented Mar 29, 2017

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

@tsvetomir
Copy link
Author

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

@tbosch tbosch added area: core Issues related to the framework runtime severity3: broken type: bug/fix freq2: medium area: common Issues related to APIs in the @angular/common package and removed area: core Issues related to the framework runtime labels Apr 10, 2017
@tbosch
Copy link
Contributor

tbosch commented Apr 10, 2017

Yes, fixing ngTemplateOutlet is the right place.

@tbosch tbosch added memory leak Issue related to a memory leak and removed severity3: broken labels Apr 10, 2017
@tbosch tbosch self-assigned this May 5, 2017
@tbosch
Copy link
Contributor

tbosch commented May 5, 2017

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

tbosch added a commit to tbosch/angular that referenced this issue May 5, 2017
tyler-gh pushed a commit to lucidsoftware/angular that referenced this issue May 5, 2017
tbosch added a commit to tbosch/angular that referenced this issue May 8, 2017
This also clarifies via a test 
that we only update projected views when the view is created or destroyed,
but not when it is attached/detached/moved.

Fixes angular#15578
@mhevery mhevery closed this as completed in f0f6544 May 8, 2017
jasonaden pushed a commit to jasonaden/angular that referenced this issue May 10, 2017
…gular#16592)

This also clarifies via a test 
that we only update projected views when the view is created or destroyed,
but not when it is attached/detached/moved.

Fixes angular#15578

PR Close angular#16592
@sgrinich
Copy link

sgrinich commented Jun 16, 2017

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.

@tsvetomir
Copy link
Author

tsvetomir commented Jun 18, 2017

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

@sgrinich
Copy link

Ok, thanks @tsvetomir

asnowwolf pushed a commit to asnowwolf/angular that referenced this issue Aug 11, 2017
…gular#16592)

This also clarifies via a test 
that we only update projected views when the view is created or destroyed,
but not when it is attached/detached/moved.

Fixes angular#15578

PR Close angular#16592
juleskremer pushed a commit to juleskremer/angular that referenced this issue Aug 28, 2017
…gular#16592)

This also clarifies via a test 
that we only update projected views when the view is created or destroyed,
but not when it is attached/detached/moved.

Fixes angular#15578

PR Close angular#16592
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: common Issues related to APIs in the @angular/common package freq2: medium memory leak Issue related to a memory leak type: bug/fix
Projects
None yet
Development

No branches or pull requests

6 participants