WittCode💻

JavaScript Memory Management

By

Learn about JavaScript memory management including the memory life cycle, how memory is freed, how memory is allocated, how memory is written to and read from, how programs work with RAM, and JavaScript garbage collection with the mark and sweep algorithm.

Table of Contents 📖

Memory Management

In computer programming, memory management is controlling how a program works with RAM. RAM capacity is not infinite so when a program is loaded into memory the space it uses needs to be managed. If a program consumes too much memory without freeing it, then it will run out of memory and crash. Low level languages like C rely on manual memory allocation and release. Higher level languages like JavaScript have automatic memory management.

Memory Life Cycle

Both manual and automatic memory management follow the basic memory life cycle. The memory life cycle consists of three steps.

  • Allocate memory
  • Read from and write to the allocated memory
  • Free the allocated memory when it is no longer used

JavaScript Memory Allocation

The first step of the memory life cycle, allocation, is automatically handled by JavaScript when values are declared intially or created through functions.

const myNum = 27; // Memory allocated for number const myBool = true; // Memory allocated for boolean const myString = "hello"; // Memory allocated for string

// Memory allocated for object and values const myObj = { name: "WittCode", soccer: "fun" };

// Memory allocated for object and contained values // In JavaScript, an array is an object const myArray = [1, 2, 3];

// Memory allocated for new object from array concatenation const myNewArray = myArray.concat([4, 5, 6]);

// Memory allocated for object // In JavaScript, a function is an object function myFunction() { return "Hello!"; }

Here, each value will need allocated memory when it is declared.

JavaScript Read From and Write to Memory

The second stage of the memory life cycle, reading from and writing to memory, is handled automatically by JavaScript when working with declared values. For example, passing a variable to a function, reading the value of a variable, changing the property of an object, etc.

// Working with the myNum variable console.log(myNum); // Working with myObj object myObj.soccer = "Not fun";

Here, we are reading from memory by reading the value stored within myNum and writing to memory by changing the property of an object.

JavaScript Freeing Memory

In JavaScript, freeing allocated memory occurs during garbage collection, a form of automatic memory management. A garbage collector determines if values are stilled needed. If they aren't needed, that allocated piece of memory is freed. There are different algorithms that garbage collectors use to determine if a block of allocated memory is no longer needed. For example, the mark and sweep algorithm frees up memory if the object taking it up is unreachable. In Javascript, this is done by starting at the global object and traversing down it. If there are objects present with no reference then they are removed.

// Memory allocated for object const myObj = { name: "WittCode", soccer: "fun" };

// Reference to myObj is removed, object can now be garbage collected myObj = null;

Here, setting the myObj reference to null makes the object referenced by myObj unreachable. Therefore, the mark and sweep algorithm will detect this, marking it for garbage collection. Memory can also be freed when allocated values are out of scope, such as after a function call has completed.

function myFunction() { // Memory allocated for object const myObj = { name: "WittCode", soccer: "fun" }; }

myFunction(); // Function call has ended, object inside myFunction can be garbage collected

Here, the variable declared inside myFunction can be garbage collected after the function call has completed. This is because after the myFunction call has ended, the reference to myObj is no longer needed as it was declared locally to the function.