I try to understand how the GabageCollector works after a component was destroyed. I have the following case:
AppComponent:
export class AppComponent {
testCompoments = [1,2];
removeOne(id){
this.testCompoments.pop()
}
nop(){
}
}
AppHTML:
<ng-container *ngFor="let t of testCompoments">
<app-test [id]="t" (close)="removeOne($event)"></app-test>
</ng-container>
<br>
EMPTY FN
TestComponent:
export class TestComponent implements OnInit {
@Output()
public close = new EventEmitter()
@Input()
id;
constructor() { }
ngOnInit() {
}
closeClick(){
this.close.emit(this.id)
}
}
TestHTML:
<p>{{id}}</p>
<button (click)="closeClick()">close</button>
Now when i start the app and take a heap-snapshot i see the right amount of objects in memory:
Then i remove a component by clicking the close-button and take a new snapshot:
To my surprise, there are still two objects? And now additionally the strange thing is, if i click the "EMPTY FN" button, which does nothing (empty-function on app-component), the next snapshot looks like this:
I have a few questions about this topic:
It seems Chrome keeps one instance in memory for ngFor. Memory leak isn't as severe as it would seem.
https://angular-garbage-collection-components.stackblitz.io/
By comparing snapshots on Chrome:
Code for reference: https://stackblitz.com/edit/angular-garbage-collection-components
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.