注意:jdk14中G1代码有极大不同
RSet中遍历分区
bool ScanRSClosure::doHeapRegion(HeapRegion* r) {
HeapRegionRemSet* hrrs = r->rem_set();
if (hrrs->iter_is_complete()) return false;
if (!_try_claimed && hrrs->claim_iter()) return false;
_g1h->push_dirty_cards_region(r);//更新RSet
HeapRegionRemSetIterator iter(hrrs);
size_t card_index;
//一次扫描分区数,默认64
size_t jump_to_card = hrrs->iter_claimed_next(_block_size);
for (size_t current_card = 0; iter.has_next(card_index); current_card++) {
if (current_card >= jump_to_card + _block_size) {
jump_to_card = hrrs->iter_claimed_next(_block_size);
}
if (curent_card < jump_to_card) continue;
HeapWord* card_start = _g1h->bot_shared()->address_for_index(card_index);
HeapRegion* card_region = _g1h->heap_region_containing(card_start);//引用者分区
_cards++;
//不在CSet才需要更新,在CSet会被回收
if (!card_region->in_collection_set() && !_ct_bs->is_card_dirty(card_index)) {
scanCard(card_index, card_region);
}
}
//..
}
对每一个分区查找对应引用了此分区的对象
void HeapRegionDCTOC::walk_mem_region(...) {
G1CollectedHeap* _g1;
size_t oop_size;
HeapWord* cur = bottom;//pointer to the first object
if (!g1h->is_obj_dead(oop(cur), _hr)) {
oop_size = oop(cur)->oop_iterate(_rs_scan, mr);
} else {
oop_size = _hr->block_size(cur);
}
cur += oop_size;
if (cur < top) {
oop cur_oop = oop(cur);
oop_size = _hr->block_size(cur);
HeapWord* next_obj = cur + oop_size;
while(next_obj < top) {
if (!g1h->is_obj_dead(oop(cur), _hr)) {
cur_oop->oop_iterate(_rs_scan);
}
cur = next_obj;
//..
//主要有dummy填充,while起来挺快的
}
}
//最后一个对象可能跨区域
if (!g1h->is_obj_dead(oop(cur), _hr)) {
oop_size = oop(cur)->oop_iterate(_rs_scan, mr);
}
}
对对象的字段查找
template <class T> inline void G1ParPushHeapRSClosure::do_oop_nv(T* p) {
T heap_oop = oopDesc::load_heap_oop(p);
if (!oopDesc::is_null(heap_oop)) {
oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
if (_g1->is_in_cset_or_humongous(obj)) {
_par_scan_state->push_on_queue(p);
}
}
}