Clear mark-bitmap after compaction

It was mistakenly not being cleared until next GC cycle, in which it
would then immediately be used again, unnecessarily increasing private
dirty.

Bug: 263341475
Bug: 263326265
Bug: 160737021
Test: crystallbal and MPTS
Change-Id: I0e900636431f49bde6368bf8c0d1e28393289d08
diff --git a/runtime/gc/collector/mark_compact.cc b/runtime/gc/collector/mark_compact.cc
index 38cbcb7..226c1b2 100644
--- a/runtime/gc/collector/mark_compact.cc
+++ b/runtime/gc/collector/mark_compact.cc
@@ -242,7 +242,9 @@
     : GarbageCollector(heap, "concurrent mark compact"),
       gc_barrier_(0),
       mark_stack_lock_("mark compact mark stack lock", kMarkSweepMarkStackLock),
+      non_moving_space_(heap->GetNonMovingSpace()),
       bump_pointer_space_(heap->GetBumpPointerSpace()),
+      moving_space_bitmap_(bump_pointer_space_->GetMarkBitmap()),
       moving_to_space_fd_(kFdUnused),
       moving_from_space_fd_(kFdUnused),
       uffd_(kFdUnused),
@@ -435,14 +437,9 @@
       // in these spaces. This card-table will eventually be used to track
       // mutations while concurrent marking is going on.
       card_table->ClearCardRange(space->Begin(), space->Limit());
-      if (space == bump_pointer_space_) {
-        // It is OK to clear the bitmap with mutators running since the only
-        // place it is read is VisitObjects which has exclusion with this GC.
-        moving_space_bitmap_ = bump_pointer_space_->GetMarkBitmap();
-        moving_space_bitmap_->Clear();
-      } else {
-        CHECK(space == heap_->GetNonMovingSpace());
-        non_moving_space_ = space;
+      if (space != bump_pointer_space_) {
+        CHECK_EQ(space, heap_->GetNonMovingSpace());
+        CHECK_EQ(non_moving_space_, space);
         non_moving_space_bitmap_ = space->GetMarkBitmap();
       }
     }
@@ -3752,6 +3749,11 @@
 
   info_map_.MadviseDontNeedAndZero();
   live_words_bitmap_->ClearBitmap();
+  // TODO: We can clear this bitmap right before compaction pause. But in that
+  // case we need to ensure that we don't assert on this bitmap afterwards.
+  // Also, we would still need to clear it here again as we may have to use the
+  // bitmap for black-allocations (see UpdateMovingSpaceBlackAllocations()).
+  moving_space_bitmap_->Clear();
 
   if (UNLIKELY(is_zygote && IsValidFd(uffd_))) {
     heap_->DeleteThreadPool();
diff --git a/runtime/gc/collector/mark_compact.h b/runtime/gc/collector/mark_compact.h
index 498a56d..cf78ffa 100644
--- a/runtime/gc/collector/mark_compact.h
+++ b/runtime/gc/collector/mark_compact.h
@@ -628,11 +628,11 @@
   size_t last_checked_reclaim_page_idx_;
   uint8_t* last_reclaimed_page_;
 
-  // The main space bitmap
-  accounting::ContinuousSpaceBitmap* moving_space_bitmap_;
-  accounting::ContinuousSpaceBitmap* non_moving_space_bitmap_;
-  space::ContinuousSpace* non_moving_space_;
+  space::ContinuousSpace* const non_moving_space_;
   space::BumpPointerSpace* const bump_pointer_space_;
+  // The main space bitmap
+  accounting::ContinuousSpaceBitmap* const moving_space_bitmap_;
+  accounting::ContinuousSpaceBitmap* non_moving_space_bitmap_;
   Thread* thread_running_gc_;
   // Array of pages' compaction status.
   Atomic<PageState>* moving_pages_status_;