| Memory Leak |
Article Index for Memory |
Website Links For Memory |
Information AboutMemory Leak |
| CATEGORIES ABOUT MEMORY LEAK | |
| programming bugs | |
| software anomalies | |
|
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 original program 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. 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. 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
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. 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 may look simple enough, but it does have a memory leak. Consider the case where the lift is already on floor 3, and button 3 is pressed. We get some memory, and never give it back. Each time this happens, we leak a little more. This will not have any immediate effect. People don't 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. But eventually the lift will 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 opening. Notice that the memory leak would only last as long as the program was running. For example, if a power cut affected the lift, then when the power came back, the memory would all be available and the slow process of leaking would have to 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 Tool s to detect unreachable memory. '' Purify '', '' Valgrind '', '' Insure++ '' and '' Memwatch '' are some of the more popular Memory Debugger s for C and C++ programs. It should be noted that 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. Languages that provide automatic memory management, like Java , C# 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. 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
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:
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. 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 they ''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 . 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, it will eventually fail when no more memory is available to the program. int f(void) {
else
}
} int main(void) {
return 0; } SEE ALSO EXTERNAL LINKS
|
|
|