Garbage collection: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Corrected case)
No edit summary
Line 1: Line 1:
[[Category:Encyclopedia]]'''Garbage collection''' (sometimes called "automatic memory management") is a feature of some [[programming language|programming languages]] (such as [[Java]], [[Tcl]], and [[Toka]]) which manages memory allocation so that memory locations that are no longer in use by the program are "given back" to the [[OS|operating system]].
[[Category:Encyclopedia]]'''Garbage collection''' (often abbreviated as '''GC''') is a technique used for management of the life time of the object created [[run time|dynamically]], which scope is [[compile time|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 language|programming languages]] (such as [[Java]], [[Tcl]], and [[Toka]]) have integrated GC support.
A memory location is no longer in use when there are no [[reference|references]] to it in use anymore. For instance (in a language with scope):

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 [[reference|referencing]] it. For instance (in a language with scope):
for i = 1 to 10
for i = 1 to 10
var x = i * i
var x = i * i

Revision as of 09:19, 26 August 2008

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.