Garbage Collectors in JavaScript

Garbage Collectors in JavaScript

In this article, we will analyze the basic principles of clearing memory in JS, as well as consider several garbage collection algorithms and determine which one is more efficient.

Memory clearing is a mandatory process for any application, including those written in JS. If we neglect this process, the memory will be filled with objects (mostly “dead” ones), and at some point, there will simply be no place for new data. In this article, we will analyze the basic principles of clearing memory in JS, as well as consider several garbage collection algorithms and determine which one is more efficient.

What Garbage Is

Garbage is any object that cannot be reached by reference from the “root”. Garbage includes all “dead objects” that have lost contact with the root ones.

Moreover, “dead” objects can often be linked with each other and form chains of considerable dimensions. Still, it doesn’t make them “live,” and that’s why a reference counting approach to garbage collection doesn’t work. But which ones work then? Let's start at the beginning.

Approaches to Garbage Collection

Let’s consider several approaches, starting with the simplest and fastest and ending with the most effective but slowest ones. 

Mark and Sweep

This is the easiest garbage search and deletion algorithm. It works only in a situation where the memory contains garbage; if it is only filled with live objects, Mark and Sweep doesn’t do anything.

The main drawback of Mark and Sweep is that it fragments memory. As a result, it can clear a lot of space, but still, a new large object can’t be stored in RAM.

javascript web-development programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

What Garbage Collection in JavaScript Is and How It Works

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.

What Javascript Spread Operator is, How It Works and How to Use It

JavaScript spread operator is one of the more popular features that were introduced in ES6. This tutorial will help you understand it. You will learn what spread operator is and how it works. You will also learn how to use it to copy and merge arrays and object literals, insert data and more.

Top 10 Web Development Trends in 2020 | Web Development Skills

Top 10 Web Development Trends in 2020 will provide you with a detailed list of the trends which are currently being noticed. Upskilling to these trends will help you in landing onto better jobs in 2020-2021.