question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

listeners invoked inside embedded views cause a memory leak

See original GitHub issue

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

Don’t known / other

Is this a regression?

Yes

Description

When a component has a button with click handler and ngIf it won’t be destroyed after user click

Please provide a link to a minimal reproduction of the bug

https://github.com/s3141p/Ng-leak

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in

Angular CLI: 12.1.1
Node: 14.15.1
Package Manager: npm 6.14.8
OS: linux x64

Angular: 12.1.1
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1201.1
@angular-devkit/build-angular   12.1.1
@angular-devkit/core            12.1.1
@angular-devkit/schematics      12.1.1
@schematics/angular             12.1.1
rxjs                            6.6.7
typescript                      4.2.4

Anything else?

To reproduce:

  1. Clone the repo
  2. ng serve
  3. Click on To B Component link
  4. Click on B button (!! Important step)
  5. Make Memory snapshot
  6. Click on To A Component
  7. Run GC
  8. Make Memory snapshot

Last snapshot will show that BComponent wasn’t destroyed.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
CalinaCristiancommented, Nov 16, 2021

is this issue being solved soon? These leaks get very big if you’re unit testing your application.

4reactions
petebacondarwincommented, Jul 14, 2021

Thanks for the awesome reproduction @s3141p - this bit is 75% of the effort 😃

Looking at the generated Ivy code for the button click. Because it is in a template (due to the *ngIf) we get the following:

ɵɵlistener("click", function BComponent_button_2_Template_button_click_0_listener() {
  ɵɵrestoreView(_r2);
  const ctx_r1 = ɵɵnextContext();
  return ctx_r1.doNothing();
});

Both the call to ɵɵrestoreView() and ɵɵnextContext() attach objects to the current LFrame.contextLView property. The second call is the one responsible for attaching the LComponentView_BComponent object.

It seems like when the BComponent is destroyed we are not clearing this contextLView property.

One solution might be for the generated code to clear the current view at the end of the handler:

ɵɵlistener("click", function BComponent_button_2_Template_button_click_0_listener() {
  ɵɵrestoreView(_r2);
  const ctx_r1 = ɵɵnextContext();
  const result = ctx_r1.doNothing();
  ɵɵleaveView();
  return result;
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS : How to debug "EventEmitter memory leak detected ...
In my case when I received 5 data streams, the socket.on('close') listener is waiting for a close event to happen. When I close...
Read more >
Debugging Memory Leaks in Angular | by Giancarlo Buomprisco
Event Listeners​​ Another common cause of memory leaks is DOM events that are never unregistered. Some folks may think that using Angular's ...
Read more >
Causes of Memory Leaks in JavaScript and How to Avoid Them
Keeping redundant objects in memory results in excessive memory use inside the app and can lead to degraded and poor performance.
Read more >
How to get rid of memory leaks? A practical approach using ...
LeakCanary separates the leaks it finds in your app into two categories: Application Leaks and Library Leaks. A library leak is a leak...
Read more >
Understanding Memory Leaks In Programming - Medium
It's an indication that a program object has left a “footprint” in memory. the user of the program experiences a degrade in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found