Application stays in memory even after being destroyed
See original GitHub issueWhich @angular/* package(s) are the source of the bug?
platform-browser
Is this a regression?
Yes
Description
I am not sure of the source module but it is from Angular application.
I have created a basic Angular application using ng new command. Then I only modify the app.component.ts file. The same is written below:
`import { Component, HostListener, OnDestroy } from ‘@angular/core’; import { platformBrowser } from ‘@angular/platform-browser’;
@Component({ selector: ‘app-root’, templateUrl: ‘./app.component.html’, styleUrls: [‘./app.component.css’] }) export class AppComponent implements OnDestroy { ngOnDestroy(): void { console.log(‘ngOnDestroy called’); } @HostListener(‘window:message’,[‘$event’]) onMessage(e:any) { console.log(e); if(e[“data”] == “close”) this.closeForm(); } closeForm() { platformBrowser().destroy(); } title = ‘demo’; } `
I have made a index.html file which is being served by IIS. On pressing add the angular application is being loaded inside an iframe and on pressing remove button the application is being destroyed and iframe is removed from DOM.
The content of index.html file which is being served by IIS is written below:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body style="width: 100vw; height: 100vh;"> <div id="a"></div> <button onclick="add()">Add</button> <button onclick="remove()">Remove</button> </body> <script> function add() { let body = document.getElementById('a'); let rand = Math.floor(Math.random() * 1000); // body.innerHTML = `<iframe src="http://localhost/app5/#/?_dc=${rand}" title="Miscellaneous Entry Form" id="miscEntFrm" style="width:100%; height:99%; border:none;"></iframe>`; // body.innerHTML = `<iframe src="http://localhost/app1/#/misc?userId=125334675" title="Miscellaneous Entry Form" id="miscEntFrm" style="width:100%; height:99%; border:none;"></iframe>`; body.innerHTML = `<iframe src="http://localhost:4200" title="Memory Leak Demo" id="miscEntFrm" style="width:100%; height:99%; border:none;"></iframe>`; } function remove() { document.getElementById('miscEntFrm').contentWindow.postMessage("close", "*"); setTimeout(() => {
let frame = document.getElementById('miscEntFrm');
//frame.contentWindow.location.reload();
frame.src = 'about:blank';
frame.remove();
frame = undefined;
}, 1000);
}
</script>
</html>
But the thing is, when observing memory tab performance monitor I notice that heap memory is constantly increasing when adding and removing the angular application along with iframe. Along with heap memory there is also an increase in DOM nodes, document and event listeners which decrease to some extinct but equals the previous number.
I have also build the app for production but the issue is still there.
Please provide a link to a minimal reproduction of the bug
https://github.com/rajib-g/MemoryLeakDemo.git
Please provide the exception or error you saw
OnDestroy called but application not releasing resources.
Please provide the environment you discovered this bug in (run ng version
)
Angular CLI: 13.3.6
Node: 16.15.0
Package Manager: npm 8.5.5
OS: win32 x64
Angular:
...
Package Version
------------------------------------------------------
@angular-devkit/architect 0.1303.6 (cli-only)
@angular-devkit/core 13.3.6 (cli-only)
@angular-devkit/schematics 13.3.6 (cli-only)
@schematics/angular 13.3.6 (cli-only)
Anything else?
We are developing a hybrid application where the angular application should be inside an iframe. If you need any other information please let me know.
Issue Analytics
- State:
- Created a year ago
- Comments:15 (6 by maintainers)
Top GitHub Comments
@AndrewKushnir Yes commenting out all of the console.log solves the issue. You are right that MessageEvent instance has the source property that points back to the window in the child <iframe> and that was creating the memory leakage.
Thanks for pointing out the actual issue.
I have seen various Github threads regarding memory leakage and I thought there is a high probability that this might be a regression. Some of those Github threads is given below:
Again thanks a lot for helping me to solve the issue.
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.