

{"id":67783,"date":"2019-08-14T18:24:31","date_gmt":"2019-08-14T12:54:31","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=67783"},"modified":"2019-08-14T18:24:31","modified_gmt":"2019-08-14T12:54:31","slug":"javascript-garbage-collection","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/","title":{"rendered":"JavaScript Garbage Collection &#8211; Unravel it with Diagrams &amp; Examples"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1448,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Algorithm&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251011134430\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Algorithm&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 07:14:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-12 18:01:08&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-29 13:33:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-10 21:53:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-15 18:51:45&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-10 11:22:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 11:16:56&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-24 18:37:13&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-09 09:51:39&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-04-14 08:13:07&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-07 06:17:15&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-05-07 06:17:15&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>Garbage collection is essential to learn for those who want to master any programming language, including JavaScript. The concept of memory management is one of those advanced topics that help you create efficient programs. Do you remember, we cautioned you to use global variables carefully in <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/javascript-variable-tutorial\/\">JavaScript Variables Tutorial<\/a><\/strong><\/em>? Well, this article explains the reason why. In this JavaScript garbage collection tutorial, we will cover the different approaches used by various browsers for memory management.<\/p>\n<p><em>Sit tight, be attentive, and keep in mind: you don\u2019t need to remember it all in one read. Relax and if you get stuck anywhere, try it again.<\/em><\/p>\n<h3>What is Memory Management?<\/h3>\n<p>We define various primitives, objects, functions, etc. in our program. When we do, the JavaScript engine assigns them a space in the main memory of the device. Different browsers have different techniques to manage their data. They have their tricks to determine the following three steps of memory management:<\/p>\n<ul>\n<li>Allocate memory to the variables.<\/li>\n<li>Use the defined variables.<\/li>\n<li>Remove them from the memory.<\/li>\n<\/ul>\n<p>The reason why we need to understand the concept of memory management while learning any programming language is to prevent memory leaks. In any program, memory leaks may lead to several problems, including <em>slow execution, program crashing, high latency (large delays in processing the data),<\/em> etc. It may also lead to discrepancies with other applications.<\/p>\n<p>The most important task in managing memory is to detect the unused variables in a program and remove them from memory.<br \/>\nIn low-level programming languages like C, we have functions like <strong>malloc()<\/strong> for memory allocation and the free keyword to release the memory. We don\u2019t need such approaches while working with a high-level programming language like JavaScript, as memory management is automatic and invisible to the programmer. We have no way to \u201cexplicitly\u201d release the memory from the object in JavaScript. First, we\u2019ll talk about the basic notion of memory management while programming: reachability.<\/p>\n<p><em><strong>Time to brush-up the <a href=\"https:\/\/data-flair.training\/blogs\/javascript-objects\/\">concept of JavaScript Objects<\/a><\/strong><\/em><\/p>\n<h3>Reachability of Garbage Collector<\/h3>\n<p>JavaScript uses Garbage Collector to decide whether a piece of code requires memory or not. With the help of reachable values, the engine determines when to release the memory. It cannot delete some of the values for obvious reasons:<\/p>\n<ul>\n<li>Local values of the running function.<\/li>\n<li>Values of other functions used by the current function.<\/li>\n<li>Global variables.<\/li>\n<li>Some internal values, necessary for the working of JavaScript.<\/li>\n<\/ul>\n<p>This set of inherently reachable values are roots. Any value that is accessible from a root by a reference or a chain of references is \u201creachable\u201d. The garbage collector in JavaScript monitors all the objects and various properties, removing those which are unreachable. Let\u2019s explore some examples to understand the concept better.<\/p>\n<p><strong>Case 1:<\/strong> Suppose, there is a \u201cvendor\u201d selling \u201cstrawberries\u201d in the market.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var vendor = {\r\n  fruit: \"strawberries\"\r\n};<\/pre>\n<p style=\"text-align: left\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67936\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example.png\" alt=\"Garbage Collector Reachability Example 1\" width=\"693\" height=\"347\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example.png 693w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-150x75.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-300x150.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-520x260.png 520w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/a><\/p>\n<p>If I want to have access to the strawberries, I need to buy them from the vendor. I doesn\u2019t have direct access to the fruit, but a reference to them through the vendor. What happens if the vendor has no strawberries left?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vendor = null;<\/pre>\n<p style=\"text-align: left\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67939\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-2.png\" alt=\"Garbage Collector Reachability Example 2 of strawberry &amp; vendor\" width=\"696\" height=\"348\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-2.png 696w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-2-150x75.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-2-300x150.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-2-520x260.png 520w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><\/a><\/p>\n<p>In this case, the strawberries didn\u2019t magically disappear from this world. But, that particular vendor no longer has any stock left and he displayed a board saying so. This is how we know that we cannot buy strawberries anymore. This means that there are no references to the fruit.<\/p>\n<p><strong>Case 2:<\/strong> But wait, I really do want strawberries since I love them. Also, I\u2019m a resident of this town to know where everything is. What do I do next? I ask the vendor if there is another vendor, who sells strawberries, in town. Nodding yes, he gives me the address of a lady vendor.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var vendor = {\r\nfruit: \"strawberries\"\r\n};\r\nvar ladyVendor = vendor;<\/pre>\n<p style=\"text-align: left\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67942\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-3.png\" alt=\"Garbage Collector Reachability Example 3 &amp; Case 2\" width=\"665\" height=\"346\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-3.png 665w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-3-150x78.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-3-300x156.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Garbage-Collector-Reachability-Example-3-520x271.png 520w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/a><\/p>\n<p>So, what happened here is that I got the reference to another person to gain access to the strawberries. But the next time I want to buy strawberries, I have two references to refer to, the vendor and the ladyVendor. Even if the vendor doesn\u2019t contain the reference to the strawberries anymore, I still have an option to buy them from the ladyVendor. Thus, the fruit now has two references. The image below makes it fairly clear.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reachability-of-Garbage-Collector-Code.png\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-67807 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reachability-of-Garbage-Collector-Code.png\" alt=\"Reachability of Garbage Collector Code\" width=\"1299\" height=\"742\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reachability-of-Garbage-Collector-Code.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reachability-of-Garbage-Collector-Code-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reachability-of-Garbage-Collector-Code-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reachability-of-Garbage-Collector-Code-768x439.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reachability-of-Garbage-Collector-Code-1024x585.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reachability-of-Garbage-Collector-Code-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>Now, if we want to release the memory to the object, I will have to overwrite all the values pointing to it.<\/p>\n<p>I think that you now understand how a garbage collector decides if a value is reachable or not. Let\u2019s see how it responds when it finds non-reachable objects in memory.<\/p>\n<h3>Reference Counting Garbage Collection in JavaScript<\/h3>\n<p>This is the primitive algorithm used by a garbage collector in browsers. It determines whether the value still has any object referencing it. If the object has zero references pointing to it, that object is said to be \u201cgarbage\u201d. The garbage collector detects these objects and releases them from memory.<\/p>\n<h4>Circular References<\/h4>\n<p style=\"text-align: left\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67944\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm.png\" alt=\"Reference counting Garbage Collection algorithm\" width=\"630\" height=\"370\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm.png 630w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm-150x88.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm-300x176.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm-520x305.png 520w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/p>\n<p>This is where the reference counting approach fails. The situation here is that a client is communicating with an employee of my company. This means that the client is referencing with the employee on a technical problem. On the other hand, the employee refers to the client to resolve the issue. The basic relationship is clear from the JavaScript code below.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;script&gt;\r\n      function DataFlair(employee, client){\r\n        \/\/ client and employee creating a cycle\r\n        client.assistant = employee;\r\n        employee.inquirer = client;\r\n        return {\r\n          admin: employee,\r\n          user: client\r\n        }\r\n      }\r\n      \/\/ function call with arguments\r\n      var company = DataFlair({\r\n        name: \"Mihir\" \/\/ employee name\r\n      }, {\r\n        name: \"Sameer\" \/\/ client name\r\n      });\r\n    &lt;\/script&gt;<\/pre>\n<p><strong>Screenshot:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Code.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67808\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Code.png\" alt=\"Reference Counting Garbage Collection Code\" width=\"1299\" height=\"744\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Code.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Code-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Code-300x172.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Code-768x440.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Code-1024x586.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Code-520x298.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><br \/>\nSave the program and go to your browser console to see the output. Just call the variable \u201ccompany\u201d and start clicking on the arrows at admin and user.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-67809 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Output.png\" alt=\"Reference Counting Garbage Collection Output\" width=\"1299\" height=\"744\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Output.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Output-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Output-300x172.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Output-768x440.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Output-1024x586.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-Counting-Garbage-Collection-Output-520x298.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>Notice that the two objects (employee and client) never stop referencing each other. In this scenario, the objects will never have zero references, thus staying in the memory. Even if the employee \u201cMihir\u201d leaves the company, the communication between him and \u201cSameer\u201d is open.<\/p>\n<p>If we want to prevent Mihir from communicating with the client like an assistant, we need to remove the reference of the client to the employee. Since outgoing references hold no value, the garbage collector will release the object\u2019s memory, as shown in the picture below.<\/p>\n<p style=\"text-align: left\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm-delete-object-memory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67945\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm-delete-object-memory.png\" alt=\"Reference counting Garbage Collection algorithm - delete object memory\" width=\"672\" height=\"421\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm-delete-object-memory.png 672w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm-delete-object-memory-150x94.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm-delete-object-memory-300x188.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Reference-counting-Garbage-Collection-algorithm-delete-object-memory-520x326.png 520w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/a><\/p>\n<p>We can remove these links with the help of the following statements:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">delete company.admin;\r\ndelete company.user.assistant;<\/pre>\n<h3>Mark and Sweep Garbage Collection in JavaScript<\/h3>\n<p>This is the most popular garbage collection algorithm available to the browsers. The JavaScript engine starts from roots, working its way to the references it can reach from there. The steps followed by this <a href=\"https:\/\/en.wikipedia.org\/wiki\/Algorithm\">algorithm<\/a> is as follows:<\/p>\n<ul>\n<li>The garbage collector goes through the roots, marking (remembering) them on its way.<\/li>\n<li>After it traverses all the roots, it then moves on to the references and marks them as well.<\/li>\n<li>The cycle continues until the garbage collector visits all the roots and the references associated with them. It never visits an object twice.<\/li>\n<li>After marking all the values reachable from the roots, the garbage collector removes all the objects, except the marked ones.<\/li>\n<\/ul>\n<p style=\"text-align: left\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Mark-and-sweep-Garbage-Collection-algorithm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67946\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Mark-and-sweep-Garbage-Collection-algorithm.png\" alt=\"Mark and sweep Garbage Collection algorithm\" width=\"723\" height=\"398\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Mark-and-sweep-Garbage-Collection-algorithm.png 723w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Mark-and-sweep-Garbage-Collection-algorithm-150x83.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Mark-and-sweep-Garbage-Collection-algorithm-300x165.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Mark-and-sweep-Garbage-Collection-algorithm-520x286.png 520w\" sizes=\"auto, (max-width: 723px) 100vw, 723px\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>In this JavaScript tutorial, we learned about garbage collection in JS and its working in detail. We learned a little about memory management and why we need to understand the concept. We also discussed the different algorithms used during garbage collection in JavaScript.<\/p>\n<p><em><strong>Thinking, what to learn next? Don&#8217;t worry, check out the JavaScript Errors Tutorial<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Garbage collection is essential to learn for those who want to master any programming language, including JavaScript. The concept of memory management is one of those advanced topics that help you create efficient programs.&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":67819,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18979],"tags":[20856,20855,20858,8568,20857],"class_list":["post-67783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-garbage-collector-reachability","tag-javascript-garbage-collection","tag-javascript-memory-management","tag-mark-and-sweep-garbage-collection","tag-reference-counting-garbage-collection"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Garbage Collection - Unravel it with Diagrams &amp; Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"JavaScript garbage collection tutorial covers memory management, reachability of garbage collector, its algorithms; Reference Counting &amp; Mark &amp; Sweep Garbage Collection in detail.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Garbage Collection - Unravel it with Diagrams &amp; Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"JavaScript garbage collection tutorial covers memory management, reachability of garbage collector, its algorithms; Reference Counting &amp; Mark &amp; Sweep Garbage Collection in detail.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-14T12:54:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Garbage-Collection-Tutorial.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Garbage Collection - Unravel it with Diagrams &amp; Examples - DataFlair","description":"JavaScript garbage collection tutorial covers memory management, reachability of garbage collector, its algorithms; Reference Counting & Mark & Sweep Garbage Collection in detail.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Garbage Collection - Unravel it with Diagrams &amp; Examples - DataFlair","og_description":"JavaScript garbage collection tutorial covers memory management, reachability of garbage collector, its algorithms; Reference Counting & Mark & Sweep Garbage Collection in detail.","og_url":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-08-14T12:54:31+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Garbage-Collection-Tutorial.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"JavaScript Garbage Collection &#8211; Unravel it with Diagrams &amp; Examples","datePublished":"2019-08-14T12:54:31+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/"},"wordCount":1182,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Garbage-Collection-Tutorial.jpg","keywords":["Garbage Collector Reachability","JavaScript Garbage Collection","JavaScript Memory Management","mark and sweep garbage collection","Reference Counting Garbage Collection"],"articleSection":["JavaScript Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/","url":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/","name":"JavaScript Garbage Collection - Unravel it with Diagrams &amp; Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Garbage-Collection-Tutorial.jpg","datePublished":"2019-08-14T12:54:31+00:00","description":"JavaScript garbage collection tutorial covers memory management, reachability of garbage collector, its algorithms; Reference Counting & Mark & Sweep Garbage Collection in detail.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Garbage-Collection-Tutorial.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Garbage-Collection-Tutorial.jpg","width":802,"height":420,"caption":"JavaScript Garbage Collection Tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/javascript-garbage-collection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"JavaScript Tutorial","item":"https:\/\/data-flair.training\/blogs\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"JavaScript Garbage Collection &#8211; Unravel it with Diagrams &amp; Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/67783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=67783"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/67783\/revisions"}],"predecessor-version":[{"id":67947,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/67783\/revisions\/67947"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/67819"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=67783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=67783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=67783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}