Cheney's algorithm: Difference between revisions

Content deleted Content added
copy edit with General fixes, removed stub tag
Adding short description: "Computer software algorithm"
 
(20 intermediate revisions by 13 users not shown)
Line 1:
{{Short description|Computer software algorithm}}
{{Use dmy dates|date=August 2012}}
{{moreUse footnotesdmy dates|date=AprilSeptember 20142021}}
{{more footnotes|date=April 2014}}
'''Cheney's algorithm''', first described in a 1970 [[Association for Computing Machinery|ACM]] paper by C.J. Cheney, is a [[stop and copy]] method of [[tracing garbage collection]] in computer software systems. In this scheme, the [[Memory management#DynamicManual memory allocationmanagement|heap]] is divided into two equal halves, only one of which is in use at any one time. Garbage collection is performed by copying live objects from one semispace (the from-space) to the other (the to-space), which then becomes the new heap. The entire old heap is then discarded in one piece. It is an improvement on the previous stop -and -copy technique.{{citation needed|date=April 2016}}
 
Cheney's algorithm reclaims items as follows:
* '''Object references on the stack.''' Object references on the stack are checked. One of the two following actions is taken for each object reference that points to an object in from-space:
** If the object has not yet been moved to the to-space, this is done by creating an identical copy in the to-space, and then replacing the from-space version with a forwarding pointer to the to-space copy. Then update the object reference to refer to the new version in to-space.
** If the object has already been moved to the to-space, simply update the reference from the forwarding pointer in from-space.
* '''Objects in the to-space.''' The garbage collector examines all object references ''in the objects that have been migrated to the to-space'', and performs one of the above two actions on the referenced objects.
Line 11 ⟶ 12:
Once all to-space references have been examined and updated, garbage collection is complete.
 
The algorithm needs no stack and only two pointers outside of the from-space and to-space: a pointer to the beginning of free space in the to-space, and a pointer to the next word in to-space that needs to be examined. ForThe thisdata reason,between itthe is sometimes called a "two-finger" collector—itpointers onlyrepresents needswork "tworemaining fingers"for pointing into theit to-space todo keep(those trackobjects ofare its''gray'' state. The data betweenin the twotri-color fingersterminology, representssee work remaining for it to dolater).
 
The forwarding pointer (sometimes called a "broken heart") is used only during the garbage collection process; when a reference to an object already in to-space (thus having a forwarding pointer in from-space) is found, the reference can be updated quickly simply by updating its pointer to match the forwarding pointer.
 
Because the strategy is to exhaust all live references, and then all references in referenced objects, this is known as a ''[[breadth-first]]'' list copying garbage collection scheme.
 
==Sample algorithm==
 
<pre>
initialize() =
tospace = N/2
fromspace = 0
allocPtr = fromspace
scanPtr = whatever -- only used during collection
</pre>
 
<pre>
allocate(n) =
If allocPtr + n > fromspace+ N/2
collect()
EndIf
If allocPtr + n > fromspace+ N/2
fail “insufficient memory”
EndIf
o = allocPtr
allocPtr = allocPtr + n
return o
</pre>
 
<pre>
collect() =
swap(fromspace, tospace)
allocPtr = fromspace
scanPtr = fromspace
 
-- scan every root you've got
ForEach root in the stack -- or elsewhere
root = copy(root)
EndForEach
-- scan objects in the heap (including objects added by this loop)
While scanPtr < allocPtr
ForEach reference r from o (pointed to by scanPtr)
r = copy(r)
EndForEach
scanPtr = scanPtr + o.size() -- points to the next object in the heap, if any
EndWhile
 
</pre>
 
<pre>
copy(o) =
If o has no forwarding address
o' = allocPtr
allocPtr = allocPtr + size(o)
copy the contents of o to o'
forwarding-address(o) = o'
EndIf
return forwarding-address(o)
 
</pre>
 
== Semispace ==
Line 79 ⟶ 24:
== Equivalence to tri-color abstraction ==
 
Cheney's algorithm is an example of a [[Tracingtracing garbage collection#Tri-color marking|tri-color marking]] garbage collector. The first member of the gray set is the stack itself. Objects referenced on the stack are copied into the to-space, which contains members of the black and gray sets.
 
The algorithm moves any white objects (equivalent to objects in the from-space without forwarding pointers) to the gray set by copying them to the to-space. Objects that are between the scanning pointer and the free-space pointer on the to-space area are members of the gray set still to be scanned. Objects below the scanning pointer belong to the black set. Objects are moved to the black set by simply moving the scanning pointer over them.
Line 87 ⟶ 32:
== References ==
* {{cite journal
| last = Cheney | first = C.J.
| title = A Nonrecursive List Compacting Algorithm
| journal = Communications of the ACM
| volume = 13 | issue = 11 | date = November 1970 | pages = 677–678
| url = http://doi.acm.org/=10.1145/362790.362798
|s2cid=36538112
| doi = 10.1145/362790.362798
|doi-access=free
}}
* {{cite journal
| last last1= Fenichel | first first1= R.R.
|last2=Yochelson |first2 = Jerome C.
| title = A LISP garbage-collector for virtual-memory computer systems
| journal = Communications of the ACM
| volume = 12 | issue = 11 |year=1969 | pages = 611–612
| url = http://portal.acm.org/citation.cfm?id=363280
| doi = 10.1145/363269.363280
|s2cid=36616954
| last2 = Yochelson
|doi-access=free
| first2 = Jerome C.
}}
* {{cite journal
| last =Byers Byers| first = Rick
| title = Garbage Collection Algorithms
| journal = Garbage Collection Algorithms
| volume = 12 | issue = 11 |year=2007 | pages = 3–4
| url = http://courses.cs.washington.edu/courses/csep521/07wi/prj/rick.pdf
}}
 
* [http://www.cs.umd.edu/class/fall2002/cmsc631/cheney/cheney.html Tutorial] at the [[University of Maryland, College Park]]
== External links ==
* {{youtube|1uLzSXWWfDg|Understanding Android Runtime (Google I/O'19)}} - Android uses a variant of the semi-space garbage collector.
 
{{Memory management}}
 
[[Category:Automatic memory management]]