Memory Leak

Memory Leak

A memory leak is a programming error in which a program allocates memory and never releases it again. As a result, the memory used keeps growing until the program or the entire computer slows down or crashes.

Every computer has working memory. This is the fast temporary storage where a running program keeps its data for as long as it needs it. A program constantly requests small chunks of it, for example for a loaded image or a list of names. When it no longer needs a chunk, it is supposed to give it back. A memory leak occurs when this return never happens. The program then keeps memory occupied that nobody is using anymore, and this pile keeps growing with every minute it runs.

Why the computer suddenly crawls after hours

A memory leak isn’t noticeable at first. If a program loses only a few kilobytes per minute, you won’t notice anything during testing. Only after hours or days of continuous operation does the loss add up to gigabytes. That’s exactly why memory leaks are so treacherous: they slip past the usual short tests developers run without being noticed.

When working memory fills up, the operating system reacts. It moves data onto the much slower hard drive, which makes the whole computer sluggish. If that’s still not enough, the system forcibly terminates the program. A mobile game that simply closes after an hour is a typical symptom.

This becomes especially costly on servers. A service that answers millions of requests per day is ideally supposed to run continuously for weeks. Some companies work around this by routinely restarting their servers at night. That’s not a solution, just a band-aid over a bug nobody has found.

How memory gets lost

You can picture working memory as a shelf in a library. A program reserves a slot and puts something in it. Afterward, it keeps a note of which slot the thing is in. That note is exactly the weak point. If the program throws away the note without clearing the slot first, the slot is blocked forever. Nobody knows anymore what’s in it, and nobody can free it up.

In languages like C or C++, the programmer has to clear every slot themselves. If they forget a single spot in a loop that runs millions of times, a leak is created. More modern languages like Java, Python, or JavaScript have an automatic garbage collector for this. It clears away all slots for which no note exists anymore.

Even so, leaks still occur there too, just differently. They arise when the program holds onto notes it no longer needs at all. A common case is a list that things are only ever appended to and never removed from. To the garbage collector, this memory looks used, even though it isn’t. Such bugs can be tracked down with profilers, tools that measure which type of data is growing in memory.

From browser tabs to AI servers

Memory leaks are most commonly encountered in the browser. A web page left open for days can occupy several gigabytes. The operating system’s task manager shows this: you can see the memory used by each program there. If this number keeps steadily growing for a program without you doing more with it, that’s a clear sign.

In tech news, the term comes up in the context of updates and security vulnerabilities. A changelog often includes an entry stating that a memory leak has been fixed. Sometimes a leak is even a security problem: if memory that wasn’t freed later goes to another program, leftover remnants of old data can travel along with it. The famous Heartbleed bug from 2014 fell into this category.

The topic also plays a role in operating AI models. Such models need a great deal of memory on specialized graphics cards, and this memory is expensive and scarce. A leak in the program that manages the requests can bring a server down within a few hours. This needs to be distinguished from normally high memory demand: a model that permanently occupies a lot of memory doesn’t have a leak. A leak only exists if consumption keeps rising for no reason.

Subscribe free. Unsubscribe the second it sucks.

High-signal news across AI, business, UX, and tech. Every morning.