In computer science, a resource leak is a particular type of resource consumption by a computer program where the program does not release resources it has acquired. This condition is normally the result of a bug in a program. Typical resource leaks include memory leak and handle leak, particularly file handle leaks, though memory is often considered separately from other resources.
Examples of resources available in limited numbers to the operating system include internet sockets, file handles, process table entries, and process identifiers (PIDs). Resource leaks are often a minor problem, causing at most minor slowdown and being recovered from after processes terminate. In other cases resource leaks can be a major problem, causing resource starvation and severe system slowdown or instability, crashing the leaking process, other processes, or even the system. Resource leaks often go unnoticed under light load and short runtimes, and these problems only manifest themselves under heavy system load or systems that remain running for long periods of time.
Resource leaks are particularly a problem for resources available in very low quantities. Leaking a unique resource, such as a lock, is particularly serious, as this causes immediate resource starvation (it prevents other processes from acquiring it) and causes deadlock. Intentionally leaking resources can be used in a denial-of-service attack, such as a fork bomb, and thus resource leaks present a security bug.
Causes
Resource leaks are generally due to programming errors: resources that have been acquired must be released, but since release often happens substantially after acquisition, and many things may occur in the meantime (e.g., an exception being thrown or abnormal program termination) it is easy for release to be missed.
A very common example is failing to close files that have been opened, which leaks a file handle; this also occurs with pipes. Another common example is a parent process failing to call wait on a child process, which leaves the completed child process as a zombie process, leaking a process table entry.
Prevention and mitigation
Resource leaks can be prevented or fixed by resource management: programming techniques or language constructs may prevent leaks by releasing resources promptly, while a separate process may reclaim resources that have been leaked. Many resource leaks are fixed by resource reclamation by the operating system after the process terminates and makes an exit
system call.
Resource leaks are thus primarily a problem for long-lived processes, as leaked resources held by still-running processes are often not reclaimed; and for processes that rapidly acquire and leak many resources.
See also
References
- Deitel, Harvey M.; Deitel, Paul J. (2005), C# for Programmers (2nd ed.), Prentice Hall Professional, p. 443, ISBN 9780132465915.
- Russinovich, Mark; Solomon, David; Ionescu, Alex (2012), Windows Internals, Part 1 (6th ed.), Pearson Education, p. 279, ISBN 9780735671300.
- Gregory, Kate (1998), Using Visual C++ 6, Que Publishing, p. 590, ISBN 9780789715395.