Tuesday, July 13, 2010

Garbage Collection


Garbage Collection is the concept of collecting useless "dereferenced" memory, and freeing it for re-use by the system. De-referenced resources are those objects that are no longer in use by the program but are still allocated for use by it.

Languages like C/C++ could allow programmers to directly interact and play with memory, a responsibility that is often so abused that it does more harm than good... Problems arise when people recklessly allocate large system resources and the due to some mis-management the allocated memory is never freed. This leaves large chunks of unreacheable memory locations that ultimately cause "Memory Leaks". In comes the invention by John McCarthy, which shoulders the responsibility of memory management by de-allocating memory that is not in use by the program anymore. While the concept was initially developed for LISP only, now it has spread to a variety of High Level Languages, including updated versions of C\C++ themselves.
C++/CLI (Common Language Infrastructure), which is Microsoft's language specification has provision for both manual and automated memory management.

Garbage collector is the term used to represent automatic memory management by the system. Garbage collector scans the runtime environment for objects that are accessible directly or indirectly via references. Then it proceeds to discard all remaining objects. Typically, an object's memory is reclaimed when the number of references to it reaches zero. These scans are done in cycles, which are started automatically by the Garbage collector or when explicit calls are made to it.

Garbage collection does not guarantee immunity from memory leaks, and obviously requires a considerable percentage of system resources to run, but definitely helps programmers who have to deal with a lot of memory in their projects. Garbage collection is not commonly used in embedded projects due to their already small resource size but are available on certain platforms like .NET Micro Framework and Java-ME.

No comments:

Post a Comment