Open Closed

Approach to preventing Angular memory leaks #9980


1
dschueck created

Prerequisites

  • What is your product version? 9.3.0
  • What is your product type (Angular or MVC)? Angular and ASP.NET Core
  • What is product framework type (.net framework or .net core)? .NET Core
  • What is ABP Framework version? 5.14.0

Question / Observation

This is more of a question/observation than a specific issue. Angular apps can tend to have many .subscribe calls for Observables. An industry best practice is to manage those observable subscriptions in a way that prevents memory leaks or unexpected behavior of components. There are many references regarding this topic, but here are a few:

https://itnext.io/angular-rxjs-detecting-memory-leaks-bdd312a070a0 https://levelup.gitconnected.com/preventing-angular-subscription-memory-leaks-65f18eb9cb8e https://www.twilio.com/blog/prevent-memory-leaks-angular-observable-ngondestroy https://trungk18.com/experience/angular-common-memory-leak-use-case-observable/

When reviewing the ASP.NET Zero Angular codebase, I'm not seeing much code that implements any of the various unsubscribe best practices noted in the references above. Specifically, there are many places in the code that call the .subscribe method. However, few places in the code call .unsubscribe, use the async pipe, or any of the take/takeWhile/takeUntil options. Also, the Power Tools generate code that omit any unsubscribing approach.

Is the Angular code missing those approaches for a reason? Or is it planned for improvement in a future ASP.NET Zero release?

I'm not an Angular expert, so perhaps I'm wrong in my observations. If so, please feel free to correct my observations. Just trying to understand the ASP.NET Zero solution better so that our company can make educated decisions on potential technical improvements. :-)

Thank you.


3 Answer(s)
  • 0
    alexanderpilhar created

    Hi @dschueck,

    I was wondering about this myself a while back! I found this blog post to be an easy to understand explanation: DO I NEED TO UNSUBSCRIBE FROM AN OBSERVABLE?.

    Hope it helps ;)

  • 0
    dschueck created

    Hi @alexanderpilhar,

    Thanks for that reference blog. It also references a Stack Overflow link that adds more context and even more considerations. Even with those references and the idea of finite vs. infinite Observables, I'm thinking that one of the unsubscribing approaches should still be top-of-mind as developers use Observables.

    My main concern was that the ASP.NET Zero Angular code uses a lot of .subscribe calls without subsequently unsubscribing using any approach (in most instances). I'm a little less concerned about that now since it seems most usages are of the "finite" variety, but it still seems like an improvement area that ASP.NET Zero needs to make in their Angular approach.

  • 1
    ismcagdas created
    Support Team

    Hi Guys,

    We are using subscribe only for http requests adn those are unsubscribed automatically when the component is disposed, so it is not necessary in AspNet Zero's default usage.