What is a memory leak in programming?

NicolasBrondinBernard

Author
@NicolasBrondinBernard

A software's memory that leaks, but where does the leak start and where does it go? Where does the data go and what are the consequences?

Article published on 28/08/2023, last updated on 10/08/2026

In computing, a memory leak is a problem that occurs when software stores data in the machine's RAM, without ever deleting that data.

As the program runs, this data will grow, more or less quickly, until it saturates the RAM and forces the program to stop.

Or worse, forces the entire machine to restart.

How does it work?

Imagine a program whose sole purpose is to resize images, and which works like this:

  • It is given a path to an image
  • It opens the image's data (storing it in RAM)
  • It creates a copy of the original image, modifying the data to reduce the image by 50%
  • It saves the image in a specific folder

Let's say we give a 2MB image to our program, it will have to store in RAM:

2MB when opening the original image + 1MB for the resized image (50%)

And since we never told our program that the RAM data should be cleared after the final image has been saved, our software will keep filling up our RAM.

Until it crashes.

How to detect a memory leak?

In general, a memory leak is "easy" to detect, but not necessarily to fix. The telltale sign is the curve of RAM usage evolution as the software is used.

Here, for example, is a memory leak that happened a few months ago, and was later fixed, on the Code-Garage site:

We can clearly see the RAM consumption climbing indefinitely, even though the user load remained the same.

After the fix, we can see that RAM consumption stays at nearly the same level throughout the day!

How to prevent it?

Unfortunately, there is no miracle solution to fix, or prevent, a memory leak.

If you have no leads at all, you'll need to carefully examine your program's execution to detect any variable that could be causing a problem…

But let's still look at a few tips to minimize the chances of creating a memory leak:

Understand the language's memory management

Some programming languages have a garbage collector, a system that tracks the RAM used by a program, and automatically frees up space.

This is the case for JavaScript, C#, Java, etc…

And other languages where memory management (allocation/deallocation) must be handled manually.

C, C++, and Rust, among others.

So you don't code the same way with or without a garbage collector, and you need to know the basics of memory management for the different types of languages to avoid mistakes!

Be wary of global variables

By definition, a global variable will stay in memory as long as the program hasn't finished.

So if you store data in a global variable, such as an array for example, this data will keep growing, and will never be freed!

In some languages like JavaScript, it's even possible to accidentally create global variables, which can easily create a memory leak:

function example(){
    text = "Hello";
}
example();
console.log(text); // > Hello 

Normally, a variable declared inside a function will be freed as soon as the function finishes. But in JavaScript, if you forget to declare your variable with var, let, or const, that variable automatically becomes a global variable.

And this happens without you even realizing it!

Be careful with event listeners

When you listen for an event, like this:

var element = document.getElementById('my-button');
  element.addEventListener('click', handleClick); 

If you ever remove your button from your application, the listener will still be there and will be attached to a ghost object.

As a result, it can neither be removed manually, nor by the garbage collector, and will therefore cause a memory leak!


Trac Vu sur Unsplash

Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet