DEV Community

Bartick Maiti
Bartick Maiti

Posted on • Originally published at blog.bartick.me

Unnoticed Memory Leaks in Your JS App: A Guide for Future

As I embarked on my coding journey, the intricacies of memory management always intrigued me. Despite reading numerous articles and heeding warnings from my university professors about the perils of memory leaks, I never had the chance to witness a tangible example. In this article, I aim to demystify memory leaks by presenting a straightforward example using JavaScript.

Decoding the Memory Leak

A memory leak is a form of resource mismanagement wherein a computer program fails to appropriately handle memory allocations, leading to the retention of memory that is no longer required. In the realm of object-oriented programming, a memory leak occurs when an object persists in memory but is inaccessible to the executing code. In other words, the memory is not released, even though it is no longer required.

Crafting a Memory Leak: A Hands-On Demonstration

To illustrate a memory leak, we'll create a function that generates a closure. A closure, in this context, refers to a function that retains access to its parent scope even after the scope has concluded. The parent scope here is the createLeak function, and the child scope is the leak function.

function createLeak() {
  const leaks = [];
  function leak() {
    leaks.push("leak");
  }
  return leak;
}
Enter fullscreen mode Exit fullscreen mode

Subsequently, we'll invoke the createLeak function and store the result in a variable, effectively creating the closure.

const leak = createLeak();
Enter fullscreen mode Exit fullscreen mode

To induce a memory leak, we'll execute the leak function within a loop. This action ensures that the leaks array will never undergo garbage collection.

for (let i = 0; i < 100000; i++) {
  leak();
}
Enter fullscreen mode Exit fullscreen mode

Rectifying a Memory Leak: A Pragmatic Approach

Addressing a memory leak involves eliminating the reference to the closure. In this case, we must nullify the reference to the leak function.

leak = null;
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

This article has provided insights into both the creation and detection of memory leaks, along with practical solutions for rectification. I trust that this exploration has not only been informative but has also enhanced your comprehension of memory leaks.

Top comments (0)