A handle leak is a type of resource leak, a situation in which a finite computing resource such as memory is excessively consumed by an application. Handle leaks occur when an application opens a “handle,” a special type of reference to a system resource, and fails to close that handle when it isn't needed any longer. They have the potential to reduce performance, cause application or system crashes, and can be a symptom of major bugs or sloppy coding in a software program. Many troubleshooting tools are available to programmers to help them combat handle leaks.
In computer programming, a handle is structure that references a system resource, such as a block of memory or a file on a disk. Handles provide a layer of abstraction between system resources and applications and are usually managed by a computer’s operating system through a “handle table” that matches each handle with a resource. A programmer might use a handle to refer to a window which is then drawn on the screen by the operating system.
A well-behaved application will release handles that it no longer requires, freeing up resources and ensuring that the operating system doesn’t end up managing a resource that is not in use. Programming errors, software bugs, and incompatibilities between software can cause programs to hold onto handles they no longer need. As the application continues to run, it may accumulate a large number of unneeded handles. This is known as a handle leak.
Handle leaks, like other types of resource leaks, can lead to sluggish performance, system instability, or in extreme cases, a computer crash. This is because handles consume finite resources like system memory, and if an application generates an excessive number of handles without closing them, these resources become exhausted. The exact threshold at which a handle leak begins to cause problems depends upon the computer’s operating system, the amount of memory available, and other factors. A leak in which a few dozen handles are left open will probably not be noticed by users, while a handle leak of ten thousand or more is a sign of a serious bug in the application’s code.
Programmers need to pay special attention to handle leaks given their potential to cause problems. The most basic method of diagnosing a handle leak is to observe the handle count, which is the number of handles a process or application currently has open, as the application runs. The Windows® Task Manager can display this information, and third party utilities for both Windows® and other operating systems may also be useful in identifying and tracking down leaky handles.