class PartialMarkSweep : public MarkSweep {
public:
// Virtual as overridden by StickyMarkSweep.
GcType GetGcType() const override {
return kGcTypePartial;
}
PartialMarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix = "");
~PartialMarkSweep() {}
protected:
// Bind the live bits to the mark bits of bitmaps for spaces that aren't collected for partial
// collections, ie the Zygote space. Also mark this space is immune. Virtual as overridden by
// StickyMarkSweep.
void BindBitmaps() override REQUIRES_SHARED(Locks::mutator_lock_);
PartialMarkSweep的GC策略为kGcTypePartial。kGcTypePartial是不扫描APP进程从zygote进程继承得来的空间对象ZygoteSpace。
void PartialMarkSweep::BindBitmaps() {
MarkSweep::BindBitmaps();
WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
// For partial GCs we need to bind the bitmap of the zygote space so that all objects in the
// zygote space are viewed as marked.
for (const auto& space : GetHeap()->GetContinuousSpaces()) {
if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) {
CHECK(space->IsZygoteSpace());
immune_spaces_.AddSpace(space);
}
}
}
位于immune_spaces_中的对象将不会被追踪——除非某些对象的引用型成员变量指向了位于其他空间中的对象。PartialMarkSweep之所以比MarkSweep运行速度快,其根本原因在于要处理的空间对象较少,也就导致要处理的对象个数较少。