cover-img

Memory Management in JavaScript

20 January, 2023

2

2

0

JavaScript doesnโ€™t require the developers to manage memory manually like other low-level programming languages(for example, C). It automatically allocates memory when we create objects, and the automatic Garbage Collection process cleans it off when the memory is not used anymore.

Garbage Collection

JavaScript uses a garbage collector to manage memory. The garbage collector automatically frees the memory that is no longer being used by the program. When an object is no longer reachable, the garbage collector will mark it as eligible for garbage collection and eventually release the memory used by that object.

JavaScript's garbage collector uses a " mark-and-sweep " technique to determine which objects are no longer in use. During the mark phase, the garbage collector starts at the root objects (typically the global object and the call stack) and marks all reachable objects as in use. During the sweep phase, the garbage collector looks for objects not marked as in use and frees the memory used by those objects.

JavaScript's garbage collector can sometimes cause performance issues, mainly when a program creates and destroys many objects quickly. Several techniques can mitigate this, such as reducing the number of objects created, reusing objects, and breaking up long-running operations into smaller chunks.

Garbage Collection Algorithm

JavaScript uses a garbage collection algorithm called "Mark-and-Sweep" to manage memory automatically. The basic process of the Mark-and-Sweep algorithm is as follows:

  1. Mark phase: The garbage collector starts at the root objects (typically the global object and the call stack) and marks all reachable objects as in use.
  2. Sweep phase: The garbage collector looks for objects not marked as in use and frees the memory used by those objects.

In addition to Mark-and-Sweep, JavaScript also uses other garbage collection algorithms such as:

  1. Scavenger: A variation of the mark-and-sweep algorithm specifically designed for young objects. It is a fast, precise and non-concurrent algorithm.
  2. Generational Garbage Collection: This algorithm divides objects into young and old groups. Young objects are more likely to be garbage and are collected more frequently than old objects. This algorithm is more efficient than mark-and-sweep as it reduces the number of objects that must be examined during each collection.
  3. Concurrent Garbage Collection: This algorithm allows the garbage collector to run in parallel with the execution of the program, improving the overall performance of the program.

All the above algorithms use in JavaScript engines like V8 (Chrome and Node.js), SpiderMonkey (Firefox) and Chakra (Edge) etc.

WeakMap and WeakSet in Memory Management

In JavaScript, WeakMap and WeakSet are special types of collections that can be used to store key-value pairs (WeakMap) or individual values (WeakSet) in a way that does not prevent the garbage collector from freeing the memory used by their elements.

WeakMap:

  • A WeakMap is a collection of key-value pairs where the keys must be objects, and the values can be of any type.
  • The keys in a WeakMap are weakly referenced, meaning they do not prevent the garbage collector from freeing the memory used by the associated values.
  • When an object key is no longer reachable, it will be removed from the WeakMap along with its associated value, and the garbage collector will free the memory used by both.

WeakSet:

  • A WeakSet is a collection of individual values where all values must be objects.
  • The values in a WeakSet are weakly referenced, meaning that they do not prevent the garbage collector from freeing the memory used by them.
  • When an object value is no longer reachable, it will be removed from the WeakSet, and the garbage collector will free the memory used by it.

By using WeakMap and WeakSet, developers can store data in a way that does not prevent the garbage collector from freeing the memory used by the objects, which can help prevent memory leaks and improve the program's overall performance.

Further Reading

2

2

0

ShowwcaseHQ

San Francisco, CA, USA

Showwcase is where developers hang out and find new opportunities together as a community
Tapas Adhikary
Educator @tapaScript | Teaching JavaScript/React/FullStack | Writer | YouTuber | Founder CreoWis

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

ยฉ Copyright 2024. Showcase Creators Inc. All rights reserved.