Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does garbage collector identify roots

The first phase of a mark-sweep garbage collector is to mark(find) all the live objects on the heap. To do this, there must be a starting point or root, from where all the marking begins. How does GC identify such roots?

like image 970
Chander Shivdasani Avatar asked Jan 11 '12 00:01

Chander Shivdasani


2 Answers

"A distinguished set of objects are assumed to be reachable: these are known as the roots. Typically, these include all the objects referenced from anywhere in the call stack (that is, all local variables and parameters in the functions currently being invoked), and any global variables.

Wikipedia

like image 99
Skip Head Avatar answered Sep 21 '22 17:09

Skip Head


Using the technique called root set enumeratios all roots will be identified. Here is sentence from Mark-sweep patent document.

In the first phase, all direct references to objects from currently running programs may be identified. These references are called roots, or together a root set, and a process of identifying all such references may be called root set enumeration

Here is link for patent document. Mark sweep patent link

Eventhough it was by .NET guy, I feel Basics of mark sweep link will give you good understanding on how it works.

like image 41
kosa Avatar answered Sep 21 '22 17:09

kosa