FACTOID # 11: Oklahoma has the highest rate of women in State or Federal correctional facilities.
 
 Home   Encyclopedia   Statistics   States A-Z   Flags   Maps   FAQ   About 
   
 
WHAT'S NEW
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Memory leak

In computer science, a memory leak is a particular kind of unintentional memory consumption by a computer program where the program fails to release memory when no longer needed. The term is meant as a humorous misnomer, since memory is not physically lost from the computer. Rather, memory is allocated to a program, and that program subsequently loses the ability to access it due to program logic flaws. Computer science, or computing science, is the study of the theoretical foundations of information and computation and their implementation and application in computer systems. ... A computer program is a collection of instructions that describe a task, or set of tasks, to be carried out by a computer. ... Dynamic Memory, often called heap memory, is one of the types of memory allocation that happens inside of a computer program. ...


As is noted below, a memory leak has similar symptoms to a number of other problems, and generally can only be diagnosed by a programmer with access to the program source code; however many people are quick to describe any unwanted increase in memory usage as a memory leak, even if this is not strictly accurate. Source code (commonly just source or code) is any series of statements written in some human-readable computer programming language. ...

Contents

Consequences of a memory leak

A memory leak can diminish the performance of the computer by reducing the amount of available memory. Memory allocation is normally a component of the operating system, so the result of a memory leak is usually an ever growing amount of memory being used by the system as a whole, not merely by the erroneous process or program. Eventually, in the worst case, too much of the available memory may become allocated and all or part of the system or device stops working correctly or the application fails. This article or section does not cite its references or sources. ...


Memory leaks may not be serious or even detectable by normal means. In modern operating systems, normal memory used by an application is released when the application terminates. This means that a memory leak in a program that only runs for a short time is rarely serious.


Cases where leaks are much more serious include

  • where the program is left running, and consumes more and more memory over time (such as background tasks, on servers, but especially in embedded devices which may be left running for many years);
  • where the program is able to request memory (e.g. shared memory) that is not released, even when the program terminates;
  • where the leak is happening inside the operating system
  • where memory is very limited e.g. in an embedded system or portable device
  • where running on operating systems (such as AmigaOS) where memory may not be automatically released on termination, and if lost can only be reclaimed by a reboot

A router, an example of an embedded system. ... In computer hardware, shared memory refers to a (typically) large block of random access memory that can be accessed by several different central processing units (CPUs) in a multiple-processor computer system. ... A router, an example of an embedded system. ... AmigaOS is the default native operating system of the Amiga personal computer. ...

A layman's example

This example is intended to show how a memory leak can come about, and its effects, without needing any programming knowledge. Please note that this is a fictitious example.


The program in this case is part of some very simple software designed to control a lift (elevator). This part of the program is run whenever anyone inside the lift presses the button for a floor. A set of lifts in the lower level of a London Underground station. ...

 When a button is pressed: Get some memory, which will be used to remember the floor number Put the floor number into the memory Are we already on the target floor? If so, we have nothing to do: finished Otherwise: Wait until the lift is idle Go to the required floor Release the memory we used to remember the floor number 

This program has a memory leak. When a floor's number is pressed and the lift is already at that floor, the memory that was obtained in the second line is never given back. Each time this case occurs, more memory is leaked.


This will not have any immediate effect. People do not often press the button for the floor they are already on, and in any case, the lift might have enough spare memory that this could happen a hundred or a thousand times. However, the lift will eventually run out of memory. This could take months or years, so it might never be discovered by even the most thorough testing.


The consequences in this case would be unpleasant; at the very least, the lift would stop responding to requests to move to another floor. If the program needs memory to open the lift door, then someone may also be trapped inside, since there is no memory available for that door to open.


The memory leak would only last as long as the program was running. For example: if the lift's power were turned off the program would stop running. When power was turned on again, the program would restart and all the memory would be available again, and the slow process of leaking would start again.


Programming issues

Memory leaks are a common error in programming, especially when using languages that have no automatic garbage collection, such as C and C++. Typically, a memory leak occurs because dynamically allocated memory has become unreachable. The prevalence of memory leak bugs has led to the development of a number of debugging tools to detect unreachable memory. IBM Rational Purify, BoundsChecker, Valgrind, Insure++ and memwatch are some of the more popular memory debuggers for C and C++ programs. Garbage collection for C and C++ programs can be included as facilities and are not inherently lacking; garbage collection facilities, if included programmatically, can be used like any other programmatic feature. Computer programming (often simply programming) is the craft of implementing one or more interrelated abstract algorithms using a particular programming language to produce a concrete computer program. ... In computer science, garbage collection (also known as GC) is a form of automatic memory management. ... C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose, high-level programming language with low-level facilities. ... In computer science, dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. ... In computer science, unreachable memory is a block of memory allocated dynamically where the program that allocated the memory no longer has any reachable pointer that refers to it. ... A software bug is an error, flaw, mistake, failure, or fault in a computer program that prevents it from behaving as intended (e. ... Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware thus making it behave as expected. ... A programming tool is a program or application that software developers use to create, debug, or maintain other programs and applications. ... See also purification for the sense of refining. ... . ... Valgrind is a free programming tool for memory debugging, memory leak detection, and profiling. ... Insure++ is a memory debugger computer program, used by software developers to detect various errors in programs written in C and C++. It is made by Parasoft, and is functionally similar to other memory debuggers, such as Purify and Valgrind. ... Memwatch is a free programming tool for memory leak detection in C, released under the GNU General Public License. ... A memory debugger is a programming tool for finding memory leaks and buffer overflows. ...


Languages that provide automatic memory management, like Java, C#, VB.NET or LISP, are not immune to memory leaks. Although the memory manager can recover memory that has become unreachable and therefore logically useless, it cannot free memory that is still reachable and therefore potentially still useful. Modern memory managers therefore provide techniques for programmers to semantically mark memory with varying levels of usefulness, which correspond to varying levels of reachability. The memory manager does not free an object that is strongly reachable. An object is strongly reachable if it is reachable either directly by a strong reference or indirectly by a chain of strong references. (A strong reference is a reference that, unlike a weak reference, prevents an object from being garbage collected.) To prevent this type of memory leak, the developer is responsible for cleaning up references after use, typically by setting the reference to null once it is no longer needed and, if necessary, by unregistering any event listeners that maintain strong references to the object. Java is an object-oriented programming language developed by Sun Microsystems in the early 1990s. ... C# (pronounced see-sharp) is an object-oriented programming language developed by Microsoft as part of their . ... Visual Basic . ... Lisp is a family of computer programming languages with a long history and a distinctive fully-parenthesized syntax. ... A memory manager is a part of a computer program which accepts requests from the program to allocate and deallocate chunks of memory. ... A memory manager is a part of a computer program which accepts requests from the program to allocate and deallocate chunks of memory. ... This article discusses a general notion of reference in computing. ... In computer programming, a weak reference is a reference that does not protect the referent object from collection by a garbage collector. ... In computer programming, null is a special value for a pointer (or other kind of reference) used to signify that the pointer intentionally does not have a target. ... In computer programming, event listener is one of design patterns that is to distribute data to objects that volunteer for it, but you dont want to maintain a central list of where it should go, especially if it changes over the life of a running program. ...


In general, automatic memory management is more robust and convenient for developers, as they don't need to implement freeing routines or worry about the sequence in which cleanup is performed or be concerned about whether or not an object is still referenced. It is easier for a programmer to know when a reference is no longer needed than to know when an object is no longer referenced. However, automatic memory management can impose a small performance overhead, and it does not eliminate all of the programming errors that cause memory leaks.


Effects of a memory leak

If a program has a memory leak and its memory usage is steadily increasing, there will not usually be an immediate symptom. Almost all systems have a certain amount of available memory, which can be consumed by programs. Eventually, the available RAM may run out. This may have one of two effects

  • On systems where all memory is in RAM, there will be an immediate failure
  • Most modern operating systems on general purpose computers use a hard disk to provide virtual memory. The effect once RAM has run out is increasing use of hard disk. The hard disk can in turn eventually run out, but usually the performance of the application and/or system will have become so slow that they will be considered to have failed before that point.

If a system crashes simply because an application has continually asked for more memory, this would be considered poor system design or a bug. What will normally happen is that the program will eventually ask for memory and be refused, either because there is no more or because a per-application limit has been reached. What happens next depends on the program, and can include: Virtual memory is an addressing scheme implemented in hardware and software that allows non-contiguous memory to be addressed as if it is contiguous. ...

  • The application terminates itself, perhaps with an error message.
  • The application tries to recover from the error. In fact this is usually unsuccessful, because memory would be needed to do that, and the application has by definition lost track of how it could free the leaked memory. However, some applications will start by reserving a safety area of memory, which is freed if a failure occurs to allow some limited recovery.
  • The application assumes that the request for memory has succeeded, and continues on this basis. This will typically result in an access violation but in some cases may result in damaging information belonging to this or (in primitive systems) some other application.

Publicly accessible systems such as web servers or routers are prone to denial-of-service attacks if an attacker discovers a sequence of operations which can trigger a leak, and is known as an exploit. An access violation is the attempt by a computer process to access a memory area that it does not own or have permission to access. ... Wikimedia servers architecture The term Web server can mean one of two things: A computer that is responsible for accepting HTTP requests from clients, which are known as Web browsers, and serving them HTTP responses along with optional data contents, which usually are Web pages such as HTML documents and... This article is about a computer networking device. ... A denial-of-service attack (also, DoS attack) is an attack on a computer system or network that causes a loss of service to users, typically the loss of network connectivity and services by consuming the bandwidth of the victim network or overloading the computational resources of the victim system. ... In computer security, an exploit is a piece of software, a chunk of data, or sequence of commands that take advantage of a bug, glitch or vulnerability in order to get unintended or unanticipated behavior out of computer software, hardware, or something electronic (usually computerized). ...


Is it a memory leak?

Note that constantly increasing memory usage is not necessarily evidence of a memory leak. Some applications will store ever increasing amounts of information in memory (e.g. as a cache). If the cache can grow so large as to cause problems, this may be a programming or design error, but is not a memory leak as the information remains nominally in use. In other cases, programs may require an unreasonably large amount of memory because the programmer has assumed memory is always sufficient for a particular task; for example, a graphics file processor might start by reading the entire contents of an image file and storing it all into memory, something that is not viable where a very large image exceeds available memory. Look up cache in Wiktionary, the free dictionary. ...


To put it another way, a memory leak arises from a particular kind of programming error, and without access to the program code, someone seeing symptoms can only guess that there might be a memory leak. It would be better to use terms such as "constantly increasing memory use" where no such inside knowledge exists.


The term "memory leak" is evocative and non-programmers especially can become so attached to the term as to use it for completely unrelated memory issues such as buffer overrun. In computer programming, a buffer overflow is an anomalous condition where a program somehow writes data beyond the allocated end of a buffer in memory. ...


Simple example of unreachable memory in C

Here is a C function that deliberately leaks memory by losing the pointer to the allocated memory. Since the program loops forever calling the defective function, malloc() will eventually fail (returning NULL) when no more memory is available to the program. However, as the result returned from malloc() is ignored, the program will keep trying to allocate memory forever, and will allocate more memory if it becomes available.

 #include <stdio.h> #include <stdlib.h> void f(void) { void* s; s = malloc(50); /* get memory */ return; /* memory leak - see note below */ /* * Memory was available and pointed to by s, but not saved. * After this function returns, the pointer is destroyed, * and the allocated memory becomes unreachable. * * To "fix" this code, either the f() function itself * needs to add "free(s)" somewhere or the s needs * to be returned from the f() and the caller of f() needs * to do the free(). */ } int main(void) { /* this is an infinite loop calling the above function */ while (1) f(); /* This function call will fail to malloc sooner or later */ return 0; } 

See also

In computer security and programming, a buffer overflow, or buffer overrun, is a programming error which may result in a memory access exception and program termination, or in the event of the user being malicious, a breach of system security. ... Memory management is the act of managing computer memory. ... A memory debugger is a programming tool for finding memory leaks and buffer overflows. ...

External links

  • Detecting a Memory Leak (Using MFC Debugging Support)
  • Article "Memory Leak Detection in C++" by Cal Erickson
  • Article "Memory Leak Detection in Embedded Systems" by Cal Erickson
  • Why doesn't my application release the memory? (Java FAQ)
  • Article "Fixing Memory Leaks in KDE" by Harri Porten
  • Citations from CiteSeer

  Results from FactBites:
 
memory leak: Information from Answers.com (1943 words)
Memory allocation is normally a component of the operating system, so the result of a memory leak is usually an ever growing amount of memory being used by the system as a whole, not merely by the erroneous process or program.
Memory leaks are a common error in programming, especially when using languages that have no automatic garbage collection, such as C and [[C++]].
The term "memory leak" is evocative and non-programmers especially can become so attached to the term as to use it for completely unrelated memory issues such as buffer overrun.
Memory leak - Wikipedia, the free encyclopedia (1687 words)
In computer science, a memory leak is a particular kind of unintentional memory consumption by a computer program where the program fails to release memory when no longer needed.
Memory leaks may not be serious or even detectable by normal means.
The prevalence of memory leak bugs has led to the development of a number of debugging tools to detect unreachable memory.
  More results at FactBites »

 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m