1. 简介
在并发处理完强引用和非强引用后,ZGC就进入了转移阶段。
本文将介绍转移阶段开始的两个小步骤,重置转移集和选择转移集。
2. 源码分析
2.1 重置转移集
在标记阶段介绍后,所有的引用都已经指向对象迁移后的新地址,ZForwardingTable中的数据已经全部失效。此时需要重置转移集,为下一轮GC做准备。
hotspot/share/gc/z/zHeap.cpp
void ZHeap::reset_relocation_set() {
// 重置forwarding table
ZRelocationSetIterator iter(&_relocation_set);
for (ZForwarding* forwarding; iter.next(&forwarding);) {
_forwarding_table.remove(forwarding);
}
// 重置转移集
_relocation_set.reset();
}
重置forwarding table逻辑如下:
hotspot/share/gc/z/zForwardingTable.inline.hpp
inline void ZForwardingTable::remove(ZForwarding* forwarding) {
const uintptr_t offset = forwarding->start();
const size_t size = forwarding->size();
assert(_map.get(offset) == forwarding, "Invalid entry");
// 将offset后size个元素置为null
_map.put(offset, size, NULL);
}
转移集的reset逻辑也比较简单,就是迭代器调用forwarding的析构函数:
hotspot/share/gc/z/zRelocationSet.cpp
void ZRelocationSet::reset() {
// 析构forwardings
ZRelocationSetIterator iter