Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection - root nodes

I have recently read bits and pieces about garbage collection (mostly in Java) and one question still remains unanswered: how does a JVM (or runtime system in general) keeps track of CURRENTLY live objects?

I understand there objects are the ones which are currently on the stack, so all the local variables or function parameters, which ARE objects. The roblem with this approch is that whenever runtime system checks what currently is on the stack, how would it differentiate between a reference variable and simple int? it can't, can it?

Therefore, there must be some sort of mechanism to allow runtime to build initial list of live objects to pass for mark-sweep phase...

like image 935
Bober02 Avatar asked Dec 12 '11 22:12

Bober02


1 Answers

I found the answer provided by greyfairer is wrong. The JVM runtime does not gather the root set from stack by looking at what bytecodes are used to push data on the stack. The stack frame consists of 4 byte(32bit arch) slots. Each slot could be a reference to a heap object or a primitive value such as an int. When a GC is needed, the runtime scans the stack, from top to bottom. For each slot, it contains a reference if:

a. It's aligned at 4 byte boundary.

b. The value in the slot point to the region of the heap(between lower and upper bound).

c. The allocbit is set. The allocbit is a flag indicating whether the memory location corresponding to it is allocated or not.

Here is my reference: http://www.ibm.com/developerworks/ibm/library/i-garbage2/.

There are some other techniques to find the root set(not in Java). For example, because pointers are usually aligned at 4/8 bytes boundary, the first bit can be used to indicate whether a slot is a primitive value or pointer: for primitive values, the first bit is set to 1. The disadvantage of this is that you only have 31bits(32 bits arch) to represent the integer, and every operations on primitive values involves shifting, which is obvious an overhead.

Also, you can make all types including int allocated on the heap. That is, all things are objects. Then all slots in a stack frame are then references.

like image 59
Rainfield Avatar answered Oct 25 '22 03:10

Rainfield