{"id":324,"date":"2023-01-16T05:00:00","date_gmt":"2023-01-16T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=324"},"modified":"2024-10-21T12:37:57","modified_gmt":"2024-10-21T19:37:57","slug":"how-to-avoid-memory-leaks-with-rxjs-observables","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-avoid-memory-leaks-with-rxjs-observables\/","title":{"rendered":"How to avoid memory leaks with RxJs observables?"},"content":{"rendered":"\n<p>The easiest way to get in trouble in a big Angular application is to <a href=\"https:\/\/blog.angulartraining.com\/how-to-automatically-unsubscribe-your-rxjs-observables-tutorial-2f98b0560298#:~:text=Should%20I%20always,in%20ngOnDestroy.\" target=\"_blank\" rel=\"noopener\" title=\"\">create a memory leak by not unsubscribing from your observables<\/a>. While there are <a href=\"https:\/\/blog.angulartraining.com\/how-to-automatically-unsubscribe-your-rxjs-observables-tutorial-2f98b0560298\" target=\"_blank\" rel=\"noopener\" title=\"\">different techniques <\/a>to unsubscribe your observables automatically, one is more concise, elegant, and overall the most error-proof.<\/p>\n\n\n\n<p>That technique is to use the <a href=\"https:\/\/angular.dev\/api\/common\/AsyncPipe?tab=description\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>async<\/code> pipe<\/a> from the Angular framework.<\/p>\n\n\n\n<p><strong>Why is the <code>async<\/code> pipe such a great tool?<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, it automatically subscribes to the observable, so we no longer need to call <code>.subscribe()<\/code>.<\/li>\n\n\n\n<li>It returns the data from that observable, triggering Angular&#8217;s change detection when needed for our component to display the latest data.<\/li>\n\n\n\n<li>Finally, it automatically unsubscribes from the observable when our component is destroyed.<\/li>\n<\/ol>\n\n\n\n<p>All of that with just 6 characters! (ok, perhaps a bit more if you want to count the whitespace):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>{{myObservable | async}}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>async<\/code> pipe works exactly like the code we would write (and thus duplicate over and over again, making our code base larger and thus our code slower) if we didn&#8217;t use it. <\/p>\n\n\n\n<p>Check its <a href=\"https:\/\/github.com\/angular\/angular\/blob\/15.1.0\/packages\/common\/src\/pipes\/async_pipe.ts#L95-97\" target=\"_blank\" rel=\"noopener\" title=\"\">source code here<\/a>, and you&#8217;ll see that all it does is implement <code>ngOnDestroy<\/code> to unsubscribe from our observable.<\/p>\n\n\n\n<p>There isn&#8217;t any good reason not to use the async pipe, and if you think you have some, stay tuned for our next messages, as we&#8217;ll cover some nice tips and tricks around using that pipe.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The easiest way to get in trouble in a big Angular application is to create a memory leak by not unsubscribing from your observables. While there are different techniques to unsubscribe your observables automatically, one is more concise, elegant, and overall the most error-proof. That technique is to use the async pipe from the Angular [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,10],"tags":[],"class_list":["post-324","post","type-post","status-publish","format-standard","hentry","category-pipes","category-rxjs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=324"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/324\/revisions"}],"predecessor-version":[{"id":2202,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/324\/revisions\/2202"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}