Cookie Consent by Free Privacy Policy Generator 📌 Unsubscribing from every infinite observable... a good habit or overkill?


✅ Unsubscribing from every infinite observable... a good habit or overkill?


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

I've seen a lot of discussions123 about necessity of unsubscribing from observables during Angular component's destroying. The rule of thumb which I picked up is: when you explicitly subscribe() to an infinite observable - you are responsible for unsubscribing from it, otherwise you might end up with a memory leak due to subscriptions, which remain alive after the component is destroyed.

Of course, it doesn't relate to finite observables, such as HttpClient calls, which complete right after the request is finished. Also we are safe with async pipes, as Angular does unsubscription for us in this case.

There are various ways of unsubscribing: we can group all subscriptions into one Subscription object and then explicitly unsubscribe in ngOnDestroy or we can create a Subject, which completes on component destroy and use takeUntil operator with every subscription. Angular 16 made it even easier by introducing injectable DestroyRef and takeUntilDestroyed operator4.

Nevertheless, is it OK to mindlessly add these safety guards to every subscription?

I realized that in many cases when the target observable is a property (direct or indirect) of the same component - there are no problems with garbage collection.

Take a look at this stackblitz. For illustrative purposes FooComponent reserves a 10MB array which is referenced in the subscription to valueChanged observable of reactive form control. We can create and destroy the component several times and eventually see that the component instance, form model, 10MB array and subscription - everything is garbage collected. I used FinalizationRegistry to see it even more clearly.

Subscription to URL params from injected ActivatedRoute instance? It is cleaned up too. Angular Material paginator/sort as view children of the component? No problem either.

This does not mean that memory leaks are impossible. If your service spawns an infinite observable and you don't unsubscribe in your component - you will have a destroyed component zombie object in the heap. Until the service is destroyed, which is likely equal to 'until you quit the app'.

Things also may get more complicated if we do some processing of observable data in between, there may be a lot of examples.

However, do we need to blindly follow the rule and populate our code with takeUntil or whatever just for the sake of clarity? Can we think a little bit deeper and keep the code cleaner where possible? Or is it better to keep the habit and insert safeguards everywhere, just to be sure that we didn't miss a possible problem? What do you think?

Just after I have written this post my friend guided me to this publication by Daniel Glejzner, which provides killer arguments in favor of "keeping the habit" even in case of HttpClient observables.

  1. https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription ↩

  2. https://stackoverflow.com/questions/70093116/do-i-need-to-unsubscribe-from-an-angular-observable ↩

  3. https://stackoverflow.com/questions/61147869/do-i-need-to-unsubscribe-manually-angular-8 ↩

  4. https://angular.io/api/core/rxjs-interop/takeUntilDestroyed ↩

...

✅ Unsubscribing from every infinite observable... a good habit or overkill?


📈 119.18 Punkte

✅ The ultimate guide to unsubscribing from emails in 2024


📈 34.67 Punkte

✅ 6 Hacks to create a Good Coding Habit


📈 28.34 Punkte

✅ Would it be overkill to open tmux in every terminal?


📈 28.17 Punkte

✅ 1Password Extended Access Management: Secure Every Sign-In for Every App on Every Device [WWDC Sponsor]


📈 26.33 Punkte

✅ Make learning a bite-size habit with a lifetime subscription to Headway


📈 20.66 Punkte

✅ iOS 18 solves iCloud's irritating habit of offloading the file you need


📈 20.66 Punkte

✅ DHS CISA warns of Iranian hackers' habit of deploying data-wiping malware


📈 20.66 Punkte

✅ Daily Habit Tracker 1.0 SQL Injection


📈 20.66 Punkte

✅ Helping users and organizations build an instinctive data privacy habit


📈 20.66 Punkte

✅ The Best Habit Tracking Apps on iPhone


📈 20.66 Punkte

✅ Malware Infection at USGS Traced to Employee’s Habit of Viewing Adult Content


📈 20.66 Punkte

✅ Daily Habit Tracker 1.0 Broken Access Control


📈 20.66 Punkte

✅ 6 Steps to Making Data Reliability a Habit


📈 20.66 Punkte

✅ America’s Sanctions Habit is Hurting Peacemaking


📈 20.66 Punkte

✅ Make it a cyber-habit: Five simple steps to staying safe online


📈 20.66 Punkte

✅ [webapps] Daily Habit Tracker 1.0 - Broken Access Control


📈 20.66 Punkte

✅ Strides 15.2.1 Review: Visual habit tracking at its best


📈 20.66 Punkte

✅ [webapps] Daily Habit Tracker 1.0 - SQL Injection


📈 20.66 Punkte

✅ Develop a journaling habit with the Journey app


📈 20.66 Punkte

✅ Age verification legislation will lead to porn habit database


📈 20.66 Punkte

✅ [webapps] Daily Habit Tracker 1.0 - Stored Cross-Site Scripting (XSS)


📈 20.66 Punkte

✅ Hackers Steal Data from  KFC, Pizza Hut, Taco Bell & Habit Burger


📈 20.66 Punkte

✅ US intelligence can’t break vulnerability hoarding habit


📈 20.66 Punkte

✅ CVE-2024-24496 | Daily Habit Tracker 1.0 home.php improper authentication


📈 20.66 Punkte

✅ Hackers Steal Data from  KFC, Pizza Hut, Taco Bell & Habit Burger


📈 20.66 Punkte

✅ How I use Appwrite Databases with Pinia to build my own habit tracker


📈 20.66 Punkte

✅ Creatures of Habit Give Security Operations Teams the Upper Hand in Fighting Cybercrime


📈 20.66 Punkte

✅ CVE-2024-24495 | Daily Habit Tracker 1.0 GET Request delete-tracker.php sql injection


📈 20.66 Punkte

✅ NordVPN says installing free software is a "bad habit"


📈 20.66 Punkte

✅ Want to Join the Top 1%? Accelerate Your Business Skills with This Habit


📈 20.66 Punkte











matomo

Datei nicht gefunden!