In computer science, garbage collection is a type of memory management. It automatically cleans up unused objects and pointers in memory, allowing the resources to be used again. Some programming languages have built-in garbage collection, while others require custom functions to manage unused memory.
A common method of garbage collection is called reference counting. This strategy simply counts how many references there are to each object stored in memory. If an object has zero references, it is considered unnecessary and can be deleted to free up the space in memory. Advanced reference counting detects objects that only reference each other, which indicates the objects are unused by the parent process.
Garbage collection may also be done at compile-time, when a program's source code is compiled into an executable program. In this method, the compiler determines which resources in memory will never be accessed after a certain time. It can then add instructions to automatically deallocate those resources from memory. While this is an effective way to eliminate unused objects, it must be done conservatively to avoid deleting references required by the program.
Garbage collection is an important part of software development since it keeps programs from using up too much RAM. Besides helping programs run more efficiently, it can also prevent serious bugs, such as memory leaks, that can cause a program to crash.
Updated: January 20, 2016