Garbage collection – root nodes

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.

Leave a Comment