<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ihor Kuzmanenko</title>
    <description>The latest articles on DEV Community by Ihor Kuzmanenko (@kuzjka).</description>
    <link>https://dev.to/kuzjka</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1446641%2F927db9cd-a1d9-4c54-9af1-a7e0c0f2fc1e.png</url>
      <title>DEV Community: Ihor Kuzmanenko</title>
      <link>https://dev.to/kuzjka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kuzjka"/>
    <language>en</language>
    <item>
      <title>Unsubscribing from every infinite observable... a good habit or overkill?</title>
      <dc:creator>Ihor Kuzmanenko</dc:creator>
      <pubDate>Thu, 25 Apr 2024 09:47:15 +0000</pubDate>
      <link>https://dev.to/kuzjka/unsubscribing-from-every-infinite-observable-a-good-habit-or-overkill-4c6b</link>
      <guid>https://dev.to/kuzjka/unsubscribing-from-every-infinite-observable-a-good-habit-or-overkill-4c6b</guid>
      <description>&lt;p&gt;I've seen a lot of discussions&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;sup id="fnref3"&gt;3&lt;/sup&gt; about necessity of unsubscribing from observables during Angular component's destroying. The rule of thumb which I picked up is: when you explicitly &lt;code&gt;subscribe()&lt;/code&gt; to an &lt;em&gt;infinite&lt;/em&gt; 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. &lt;/p&gt;

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

&lt;p&gt;There are various ways of unsubscribing: we can group all subscriptions into one &lt;code&gt;Subscription&lt;/code&gt; object and then explicitly unsubscribe in &lt;code&gt;ngOnDestroy&lt;/code&gt; or we can create a &lt;code&gt;Subject&lt;/code&gt;, which completes on component destroy and use &lt;code&gt;takeUntil&lt;/code&gt; operator with every subscription. Angular 16 made it even easier by introducing injectable &lt;code&gt;DestroyRef&lt;/code&gt; and &lt;code&gt;takeUntilDestroyed&lt;/code&gt; operator&lt;sup id="fnref4"&gt;4&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Nevertheless, is it OK to mindlessly add these safety guards to &lt;em&gt;every&lt;/em&gt; subscription?&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Take a look at &lt;a href="https://stackblitz.com/edit/stackblitz-starters-2a3hou?file=src%2Ffoo%2Ffoo.component.ts"&gt;this stackblitz&lt;/a&gt;. For illustrative purposes &lt;code&gt;FooComponent&lt;/code&gt; reserves a 10MB array which is referenced in the subscription to &lt;code&gt;valueChanged&lt;/code&gt; 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 &lt;code&gt;FinalizationRegistry&lt;/code&gt; to see it even more clearly.&lt;/p&gt;

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

&lt;p&gt;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'.&lt;/p&gt;

&lt;p&gt;Things also may get more complicated if we do some processing of observable data in between, there may be a lot of examples.&lt;/p&gt;

&lt;p&gt;However, do we need to blindly follow the rule and populate our code with &lt;code&gt;takeUntil&lt;/code&gt; 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?&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Just after I have written this post my friend guided me to &lt;a href="https://dev.to/this-is-angular/always-unsubscribe-no-exceptions-debate-closed-205n"&gt;this&lt;/a&gt; publication by Daniel Glejzner, which provides killer arguments in favor of "keeping the habit" even in case of &lt;code&gt;HttpClient&lt;/code&gt; observables.&lt;/em&gt;&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription"&gt;https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/70093116/do-i-need-to-unsubscribe-from-an-angular-observable"&gt;https://stackoverflow.com/questions/70093116/do-i-need-to-unsubscribe-from-an-angular-observable&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/61147869/do-i-need-to-unsubscribe-manually-angular-8"&gt;https://stackoverflow.com/questions/61147869/do-i-need-to-unsubscribe-manually-angular-8&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;&lt;a href="https://angular.io/api/core/rxjs-interop/takeUntilDestroyed"&gt;https://angular.io/api/core/rxjs-interop/takeUntilDestroyed&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
