6

I have an angular component and inside ngOnDestroy hook I want to add asynchronous function that makes http call to delete something for example. I wonder how is memory managed and what is the scope of the callback function ? If garbage collector decides to release memory by reference does it mean that the component will stay in memory until the delete callback is complete ? Here is a stackblitz demo and examples of what I'm testing.

First:

 ngOnDestroy() {
    this.http.delete(...)
  }

Since I didnt provide .then(...) callback does it mean component can be completely unloaded from memory ?

Second:

 async ngOnDestroy() {
    await this.http.delete(...)
  }

Why does component get destroyed and doesn't await for the delete() to complete ?

Third:

 ngOnDestroy() {
    this.http.delete(...).then(console.log);
  }

Since I provided callback function does it mean component will stay in memory until the callback func completes ?

How can I see what's happening in memory allocation ? Is there a way to see what garbage collector does or maybe specify a callback function when certain object is released from memory ? Thanks

EDIT

Based on this Memory Management MDN article it appears that since 2012 Reference-counting algorithm has been replaced by Mark-and-sweep and I could expose garbage collector if I run my ng serve node process with --expose-gc flag

ihor.eth
  • 2,207
  • 20
  • 27
  • 1
    Does this answer your question? [Angular4 - how to ensure ngOnDestroy finishes before navigating away](https://stackoverflow.com/questions/47011471/angular4-how-to-ensure-ngondestroy-finishes-before-navigating-away) , https://stackoverflow.com/questions/55001261/angular-5-asynchronous-service-calls-in-two-different-components-first-method, https://stackoverflow.com/questions/46196445/how-to-wait-for-api-call-to-complete-when-called-in-ngondestroy-function – shrys May 15 '20 at 17:36
  • 2
    None of these answer my questions about memory allocation and garbage collection. Pls take time to read and understand my question before marking it as duplicate – ihor.eth May 15 '20 at 17:40

0 Answers0