listeners invoked inside embedded views cause a memory leak
See original GitHub issueWhich @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:
- Clone the repo
ng serve
- Click on
To B Component
link - Click on
B button
(!! Important step) - Make Memory snapshot
- Click on
To A Component
- Run GC
- Make Memory snapshot
Last snapshot will show that BComponent
wasn’t destroyed.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:9 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
is this issue being solved soon? These leaks get very big if you’re unit testing your application.
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:Both the call to
ɵɵrestoreView()
andɵɵnextContext()
attach objects to the currentLFrame.contextLView
property. The second call is the one responsible for attaching theLComponentView_BComponent
object.It seems like when the
BComponent
is destroyed we are not clearing thiscontextLView
property.One solution might be for the generated code to clear the current view at the end of the handler: