Hunting Memory Leakage in JavaScript Application

Hunting Memory Leakage in JavaScript Application

Memory management Unreachable pieces of memory are considered garbage, but for the rest only the developer can decide whether it's needed or not. So, the main causes of memory leaks in JavaScript are unwanted references like accidental global variables, forgotten intervals, detached DOM elements and others.

It is the process by which the program performs automatic memory management by reclaiming the memory occupied by objects that are no longer used in the program. An object when created uses memory which remains allocated until there are references for that object. The automatic accomplishment of freeing up memory occurs when these objects are considered not in use anymore, which is garbage collection, simply, destroying unused objects.

On the other hand, Manual Memory Management *refers to the usage of instruction explicitly by programmers to deallocate the memory. In C programming, developers need to take care of memory allocation and deallocation using malloc() and deallocate() functions. In JavaScript, as no interface is exposed to memory management, all the memory management is relinquished to the user. It is *garbage collection.

Memory Scheme in NodeJs

Memory Management in NodeJs is done via V8: ‘Google’s open-source high-performance JavaScript and WebAssembly engine_’_. The total memory allocated for program execution is Resident Set. This is further divided into the following segments:

Fig. Memory scheme in V8

Code

The actual code being executed.

Stack

Static data including method, primitive values, and pointers defining control flow of the program and pointers to object in the heap are stored here.

Heap

A memory segment where V8 stores objects or dynamic data. Heap is the biggest memory block dedicated to storing reference types like objects strings and closures where *Garbage Collection (GC) *takes place. Heap has two main sections:

  • New Space: *The size of the new space is ~1–8MB and objects residing here are called *New Generation.
  • Old Space: *Objects which have survived two scavenge collections now are shifted from New space to Old Spaceand called *Old Generation. After certain old space memory is allocated, expensive Garbage collection (Mark-Sweep and Mark-Compact) occur.

To get basic info on memory usage in our program, let us execute the following code in the NodeJs program and observe the output.

process.memoryUsage();

// Output (in bytes)
{

 rss: 365801472,
 heapTotal: 94416896,
 heapUsed: 79383696,
 external: 351234
}

Sources of Memory Leakage

Memory leakage is defined as a situation when the memory not needed by the program is not returned to the operating system or pool of free memory.

software-development javascript web-development reactjs nodejs

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

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

ReactJS Web App Development Services

We provide top-notch ReactJS development services to global clients. Hire expert ReactJS developers from top React JS development company, Skenix Infotech.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company