Memory leak

Memory leaks are often thought of as failures to release unused memory by a computer program. Strictly speaking, it is just unneccesary memory consumption. A memory leak occurs when the program loses the ability to free the memory. A memory leak diminishes the performance of the computer, as it becomes unable to use all its available memory.

Memory leaks are a rather common error in programming, especially when programming in languages that have no automatic embedded garbage collection (GC), like C and C++, which deeply rely on pointer operations. This has led to the development of a number of tools to detect and prevent this problem from happening. Purify, Valgrind, Insure++ and memwatch are some of the most popular tools for discovering memory leaks in C and C++ programs. It should be noted that GC for C and C++ programs can be included as facilities and are not inherently lacking; GC facilities, if included programmatically, can be used like any other programmatic feature, as Bjarne Stroustrup (the inventor of C++) points out about C++. However GC will not account for all of the programming errors that cause memory leaks.

The main issue is that it is normally a component of the operating system which is responsible for managing memory, and 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/program. Eventually, all (or too much) of the available memory may be allocated (and not freed) and the entire system (or critical subsystems) can stop working correctly.

In languages providing automatic memory management, like Java, C# or LISP there can be memory leaks too. The memory management does not free an object that is strongly reachable. This is the case if still one or more (also strongly reachable) references exist for the object. (For example, storing an object in a Vector object in Java and later losing/forgetting about the index). The developer is responsible for cleaning up references after use. Nevertheless, automatic memory management is more convenient for developers, as they don't need to implement freeing routines or worry about the sequence in which cleanup is performed. Automatic memory management does impose a small performance overhead with all the extra checking.

Simple example in C

Here is a program that deliberately leaks memory.

int
main(void)
{
   char *string1 = malloc(sizeof(char)*50);
   char *string2 = malloc(sizeof(char)*50);
   scanf("%s", string2);
   string1 = string2; 
             /*Here the memory created above for string1 is lost (leaked)
             * it is not pointed to by anything anymore, so it cannot be
             * explicitly freed
             */
   free(string2); /*This will be successful */
   free(string1); /*This will fail - it is an attempt to free 
                    memory which has already been freed */
   return 0;

}

See also

External links

es:Fuga de memoria (informática) he:דליפת זכרון ja:メモリリーク pl:Wyciek pamięci ru:Утечка памяти

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools