Garbage collection

From Rosetta Code

Garbage collection (often abbreviated as GC) is a technique used for management of the life time of the object created dynamically, which scope is statically indeterminable. Sometimes called "automatic memory management". The object that are no longer in use by the program are said to be "collected," i.e. finalized and the memory allocated for the object is returned to the language environment.

Some programming languages (such as Java, Tcl, and Toka) have integrated GC support.

An object is considered not in use when there is no legal way to access it. In particular, when there are no other accessible objects referencing it. For instance (in a language with scope):

for i = 1 to 10
  var x = i * i
  print "x is in scope since it was declared in this for block"
end for
print "x is no longer in scope and its memory location should now be given back to the OS"

Some languages (such as C and C++) do not have garbage collection and the programmer must do it himself.