Overview
From Oracle Doc :
When performing garbage collection, G1 works similarly to the CMS collector. G1 performs a parallel global marking phase to determine the viability of objects in the entire heap.
And external scanning of the root area is one of the phases of the marking process.
From Java Companion:
During this phase, external (off-heap) roots are scanned, such as the JVMs system dictionary, VM data structures, JNI thread root, hardware registers, global variables, and thread stack roots to find out if any point is present in the current set of pause sets (CSet).
Details and code
Yes, we can expect the code for g1 openjdk and hotspot to be the same as here . Therefore, we can explain the detail process by reading the source code.
From G1CollectedHeap :
void G1CollectedHeap:: g1_process_strong_roots(bool collecting_perm_gen, SharedHeap::ScanningOption so, OopClosure* scan_non_heap_roots, OopsInHeapRegionClosure* scan_rs, OopsInGenClosure* scan_perm, int worker_i) {
Then in process_strong_roots :
// Global (strong) JNI handles if (!_process_strong_tasks->is_task_claimed(SH_PS_JNIHandles_oops_do)) JNIHandles::oops_do(roots);
And the main process is about JNI: it iterates over the block of JNI descriptors and discovers that this oops descriptor block (oop: Java reference abstraction) points to the heap area, which means that this JNI-oop can be the root for gc.
for (JNIHandleBlock* current = current_chain; current != NULL; current = current->_next) { assert(current == current_chain || current->pop_frame_link() == NULL, "only blocks first in chain should have pop frame link set"); for (int index = 0; index < current->_top; index++) { oop* root = &(current->_handles)[index]; oop value = *root; // traverse heap pointers only, not deleted handles or free list // pointers if (value != NULL && Universe::heap()->is_in_reserved(value)) { f->do_oop(root); } } // the next handle block is valid only if current block is full if (current->_top < block_size_in_oops) { break; } }
And then this root is remembered in the array, OopClosure processed when the array is full, and in this case iteration of the root reference to indicate live objects.
Additionally: