Technology: Memory leaks and garbage collection


A memory leak is the gradual deterioration of system performance that occurs over time as the result of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed. Garbage collection is the even stranger term given to the automatic process, found in some systems and languages, whereby memory space no longer needed by current applications is consolidated and freed for reuse.

One of the more quaintly mysterious notions in the world of computer software, especially Windows, is the memory leak. It's so commonly encountered that leak is used as both noun and verb: "That application leaks memory like crazy." It's a major cause of system instability over time and can be a nightmare to deal with. If a program runs continually, the smallest leak will eventually and inevitably lead to a program or system crash because more and more resources get locked up until they are exhausted.

A memory leak starts when a program requests a chunk of memory from the operating system for itself and its data.

As a program operates, it sometimes needs more memory and makes an additional request. Now we come to one of the rules of good programming: any memory that is requested and allocated should be explicitly released by the application program when it no longer needs it and, in any case, when it closes.

A program that does this is called well-behaved, not all programs are. And a program's failure to delete objects properly often doesn't show up right away because the program is either a short utility or doesn't create very many instances of objects, so it takes much longer to exhaust resources.

But program objects can have other side effects that don't go away when the program terminates. A programmer should never assume that objects perform only benign operations that are undone when the program ends.

Besides, programs sometimes end unexpectedly, or crash, before they can shut down in an orderly fashion and give back their memory. The result is that pieces of memory scattered throughout the system's RAM are marked as in use and untouchable except by its owning application - even though that's not really the case. Over time, as a number of ill-behaved applications run, more and more memory leaks into this unusable state, and the amount of memory available for use gets smaller and smaller.

The operating system or system software itself isn't necessarily leakproof. (In late 1998, Apple posted a fix for an AppleScript memory leak at, the operating system finds that there's not enough memory to do almost anything it needs or wants. Then it produces an error message saying memory is low and requests that some applications be closed to free up space. But because the applications that locked up much of that memory aren't really running, you can't free up the space by closing them down. The usual fix is to reboot.

Garbage collection

Freeing up the operating system for reuse of the space that has been taken over by memory leaks is called garbage collection. In the past, programs have had to explicitly request storage and then return it to the system when it was no longer needed. The term garbage collecting appears to have first been used in the Lisp programming language, developed in the 1960s. Some operating systems provide memory leak detection so that a problem can be detected before an application or the operating system crashes.

Some program development tools, like Java, also provide automatic housekeeping for the developer. The real advantage to this is that the process happens whether or not the programmer accounts for it.

Join the newsletter!

Error: Please check your email address.
Show Comments