semi_space.cc revision b2f9936cab87a187f078187c22d9b29d4a188a62
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Copyright (C) 2011 The Android Open Source Project
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 *      http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32
33#include "semi_space.h"
34
35#include <functional>
36#include <numeric>
37#include <climits>
38#include <vector>
39
40#include "base/logging.h"
41#include "base/macros.h"
42#include "base/mutex-inl.h"
43#include "base/timing_logger.h"
44#include "gc/accounting/heap_bitmap.h"
45#include "gc/accounting/mod_union_table.h"
46#include "gc/accounting/space_bitmap-inl.h"
47#include "gc/heap.h"
48#include "gc/space/bump_pointer_space.h"
49#include "gc/space/bump_pointer_space-inl.h"
50#include "gc/space/image_space.h"
51#include "gc/space/large_object_space.h"
52#include "gc/space/space-inl.h"
53#include "indirect_reference_table.h"
54#include "intern_table.h"
55#include "jni_internal.h"
56#include "mark_sweep-inl.h"
57#include "monitor.h"
58#include "mirror/art_field.h"
59#include "mirror/art_field-inl.h"
60#include "mirror/class-inl.h"
61#include "mirror/class_loader.h"
62#include "mirror/dex_cache.h"
63#include "mirror/object-inl.h"
64#include "mirror/object_array.h"
65#include "mirror/object_array-inl.h"
66#include "runtime.h"
67#include "semi_space-inl.h"
68#include "thread-inl.h"
69#include "thread_list.h"
70#include "verifier/method_verifier.h"
71
72using ::art::mirror::Class;
73using ::art::mirror::Object;
74
75namespace art {
76namespace gc {
77namespace collector {
78
79static constexpr bool kProtectFromSpace = true;
80static constexpr bool kResetFromSpace = true;
81
82// TODO: Unduplicate logic.
83void SemiSpace::ImmuneSpace(space::ContinuousSpace* space) {
84  // Bind live to mark bitmap if necessary.
85  if (space->GetLiveBitmap() != space->GetMarkBitmap()) {
86    BindLiveToMarkBitmap(space);
87  }
88  // Add the space to the immune region.
89  if (immune_begin_ == nullptr) {
90    DCHECK(immune_end_ == nullptr);
91    immune_begin_ = reinterpret_cast<Object*>(space->Begin());
92    immune_end_ = reinterpret_cast<Object*>(space->End());
93  } else {
94    const space::ContinuousSpace* prev_space = nullptr;
95    // Find out if the previous space is immune.
96    for (space::ContinuousSpace* cur_space : GetHeap()->GetContinuousSpaces()) {
97      if (cur_space == space) {
98        break;
99      }
100      prev_space = cur_space;
101    }
102    // If previous space was immune, then extend the immune region. Relies on continuous spaces
103    // being sorted by Heap::AddContinuousSpace.
104    if (prev_space != nullptr && IsImmuneSpace(prev_space)) {
105      immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_);
106      immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_);
107    }
108  }
109}
110
111void SemiSpace::BindBitmaps() {
112  timings_.StartSplit("BindBitmaps");
113  WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
114  // Mark all of the spaces we never collect as immune.
115  for (const auto& space : GetHeap()->GetContinuousSpaces()) {
116    if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
117        || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) {
118      ImmuneSpace(space);
119    }
120  }
121  timings_.EndSplit();
122}
123
124SemiSpace::SemiSpace(Heap* heap, const std::string& name_prefix)
125    : GarbageCollector(heap,
126                       name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
127      mark_stack_(nullptr),
128      immune_begin_(nullptr),
129      immune_end_(nullptr),
130      to_space_(nullptr),
131      from_space_(nullptr),
132      soft_reference_list_(nullptr),
133      weak_reference_list_(nullptr),
134      finalizer_reference_list_(nullptr),
135      phantom_reference_list_(nullptr),
136      cleared_reference_list_(nullptr),
137      self_(nullptr) {
138}
139
140void SemiSpace::InitializePhase() {
141  timings_.Reset();
142  TimingLogger::ScopedSplit split("InitializePhase", &timings_);
143  mark_stack_ = heap_->mark_stack_.get();
144  DCHECK(mark_stack_ != nullptr);
145  immune_begin_ = nullptr;
146  immune_end_ = nullptr;
147  soft_reference_list_ = nullptr;
148  weak_reference_list_ = nullptr;
149  finalizer_reference_list_ = nullptr;
150  phantom_reference_list_ = nullptr;
151  cleared_reference_list_ = nullptr;
152  self_ = Thread::Current();
153  // Do any pre GC verification.
154  timings_.NewSplit("PreGcVerification");
155  heap_->PreGcVerification(this);
156}
157
158void SemiSpace::ProcessReferences(Thread* self) {
159  TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
160  WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
161  GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
162                               &RecursiveMarkObjectCallback, this);
163}
164
165void SemiSpace::MarkingPhase() {
166  Thread* self = Thread::Current();
167  Locks::mutator_lock_->AssertExclusiveHeld(self);
168  TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
169  // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the
170  // wrong space.
171  heap_->SwapSemiSpaces();
172  // Assume the cleared space is already empty.
173  BindBitmaps();
174  // Process dirty cards and add dirty cards to mod-union tables.
175  heap_->ProcessCards(timings_);
176  // Need to do this before the checkpoint since we don't want any threads to add references to
177  // the live stack during the recursive mark.
178  timings_.NewSplit("SwapStacks");
179  heap_->SwapStacks();
180  WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
181  MarkRoots();
182  // Mark roots of immune spaces.
183  UpdateAndMarkModUnion();
184  // Recursively mark remaining objects.
185  MarkReachableObjects();
186}
187
188bool SemiSpace::IsImmuneSpace(const space::ContinuousSpace* space) const {
189  return
190    immune_begin_ <= reinterpret_cast<Object*>(space->Begin()) &&
191    immune_end_ >= reinterpret_cast<Object*>(space->End());
192}
193
194void SemiSpace::UpdateAndMarkModUnion() {
195  for (auto& space : heap_->GetContinuousSpaces()) {
196    // If the space is immune then we need to mark the references to other spaces.
197    if (IsImmuneSpace(space)) {
198      accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
199      CHECK(table != nullptr);
200      // TODO: Improve naming.
201      TimingLogger::ScopedSplit split(
202          space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
203                                   "UpdateAndMarkImageModUnionTable",
204                                   &timings_);
205      table->UpdateAndMarkReferences(MarkRootCallback, this);
206    }
207  }
208}
209
210void SemiSpace::MarkReachableObjects() {
211  timings_.StartSplit("MarkStackAsLive");
212  accounting::ObjectStack* live_stack = heap_->GetLiveStack();
213  heap_->MarkAllocStackAsLive(live_stack);
214  live_stack->Reset();
215  timings_.EndSplit();
216  // Recursively process the mark stack.
217  ProcessMarkStack(true);
218}
219
220void SemiSpace::ReclaimPhase() {
221  TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
222  Thread* self = Thread::Current();
223  ProcessReferences(self);
224  {
225    ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
226    SweepSystemWeaks();
227  }
228  // Record freed memory.
229  int from_bytes = from_space_->GetBytesAllocated();
230  int to_bytes = to_space_->GetBytesAllocated();
231  int from_objects = from_space_->GetObjectsAllocated();
232  int to_objects = to_space_->GetObjectsAllocated();
233  int freed_bytes = from_bytes - to_bytes;
234  int freed_objects = from_objects - to_objects;
235  CHECK_GE(freed_bytes, 0);
236  freed_bytes_.fetch_add(freed_bytes);
237  freed_objects_.fetch_add(freed_objects);
238  heap_->RecordFree(static_cast<size_t>(freed_objects), static_cast<size_t>(freed_bytes));
239
240  timings_.StartSplit("PreSweepingGcVerification");
241  heap_->PreSweepingGcVerification(this);
242  timings_.EndSplit();
243
244  {
245    WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
246    // Reclaim unmarked objects.
247    Sweep(false);
248    // Swap the live and mark bitmaps for each space which we modified space. This is an
249    // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
250    // bitmaps.
251    timings_.StartSplit("SwapBitmaps");
252    SwapBitmaps();
253    timings_.EndSplit();
254    // Unbind the live and mark bitmaps.
255    UnBindBitmaps();
256  }
257  // Release the memory used by the from space.
258  if (kResetFromSpace) {
259    // Clearing from space.
260    from_space_->Clear();
261  }
262  // Protect the from space.
263  VLOG(heap)
264      << "mprotect region " << reinterpret_cast<void*>(from_space_->Begin()) << " - "
265      << reinterpret_cast<void*>(from_space_->Limit());
266  if (kProtectFromSpace) {
267    mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_NONE);
268  } else {
269    mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_READ);
270  }
271}
272
273void SemiSpace::ResizeMarkStack(size_t new_size) {
274  std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
275  CHECK_LE(mark_stack_->Size(), new_size);
276  mark_stack_->Resize(new_size);
277  for (const auto& obj : temp) {
278    mark_stack_->PushBack(obj);
279  }
280}
281
282inline void SemiSpace::MarkStackPush(Object* obj) {
283  if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
284    ResizeMarkStack(mark_stack_->Capacity() * 2);
285  }
286  // The object must be pushed on to the mark stack.
287  mark_stack_->PushBack(obj);
288}
289
290// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
291bool SemiSpace::MarkLargeObject(const Object* obj) {
292  // TODO: support >1 discontinuous space.
293  space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
294  accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
295  if (UNLIKELY(!large_objects->Test(obj))) {
296    large_objects->Set(obj);
297    return true;
298  }
299  return false;
300}
301
302// Used to mark and copy objects. Any newly-marked objects who are in the from space get moved to
303// the to-space and have their forward address updated. Objects which have been newly marked are
304// pushed on the mark stack.
305Object* SemiSpace::MarkObject(Object* obj) {
306  Object* ret = obj;
307  if (obj != nullptr && !IsImmune(obj)) {
308    if (from_space_->HasAddress(obj)) {
309      mirror::Object* forward_address = GetForwardingAddressInFromSpace(obj);
310      // If the object has already been moved, return the new forward address.
311      if (!to_space_->HasAddress(forward_address)) {
312        // Otherwise, we need to move the object and add it to the markstack for processing.
313        size_t object_size = obj->SizeOf();
314        size_t dummy = 0;
315        forward_address = to_space_->Alloc(self_, object_size, &dummy);
316        // Copy over the object and add it to the mark stack since we still need to update it's
317        // references.
318        memcpy(reinterpret_cast<void*>(forward_address), obj, object_size);
319        // Make sure to only update the forwarding address AFTER you copy the object so that the
320        // monitor word doesn't get stomped over.
321        COMPILE_ASSERT(sizeof(uint32_t) == sizeof(mirror::Object*),
322                       monitor_size_must_be_same_as_object);
323        obj->SetLockWord(LockWord::FromForwardingAddress(reinterpret_cast<size_t>(forward_address)));
324        MarkStackPush(forward_address);
325      }
326      ret = forward_address;
327      // TODO: Do we need this if in the else statement?
328    } else {
329      accounting::SpaceBitmap* object_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
330      if (LIKELY(object_bitmap != nullptr)) {
331        // This object was not previously marked.
332        if (!object_bitmap->Test(obj)) {
333          object_bitmap->Set(obj);
334          MarkStackPush(obj);
335        }
336      } else {
337        DCHECK(!to_space_->HasAddress(obj)) << "Marking object in to_space_";
338        if (MarkLargeObject(obj)) {
339          MarkStackPush(obj);
340        }
341      }
342    }
343  }
344  return ret;
345}
346
347Object* SemiSpace::RecursiveMarkObjectCallback(Object* root, void* arg) {
348  DCHECK(root != nullptr);
349  DCHECK(arg != nullptr);
350  SemiSpace* semi_space = reinterpret_cast<SemiSpace*>(arg);
351  mirror::Object* ret = semi_space->MarkObject(root);
352  semi_space->ProcessMarkStack(true);
353  return ret;
354}
355
356Object* SemiSpace::MarkRootCallback(Object* root, void* arg) {
357  DCHECK(root != nullptr);
358  DCHECK(arg != nullptr);
359  return reinterpret_cast<SemiSpace*>(arg)->MarkObject(root);
360}
361
362// Marks all objects in the root set.
363void SemiSpace::MarkRoots() {
364  timings_.StartSplit("MarkRoots");
365  // TODO: Visit up image roots as well?
366  Runtime::Current()->VisitRoots(MarkRootCallback, this, false, true);
367  timings_.EndSplit();
368}
369
370void SemiSpace::BindLiveToMarkBitmap(space::ContinuousSpace* space) {
371  CHECK(space->IsMallocSpace());
372  space::MallocSpace* alloc_space = space->AsMallocSpace();
373  accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
374  accounting::SpaceBitmap* mark_bitmap = alloc_space->BindLiveToMarkBitmap();
375  GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
376}
377
378mirror::Object* SemiSpace::GetForwardingAddress(mirror::Object* obj) {
379  if (from_space_->HasAddress(obj)) {
380    LOG(FATAL) << "Shouldn't happen!";
381    return GetForwardingAddressInFromSpace(obj);
382  }
383  return obj;
384}
385
386mirror::Object* SemiSpace::MarkedForwardingAddressCallback(Object* object, void* arg) {
387  return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
388}
389
390void SemiSpace::SweepSystemWeaks() {
391  timings_.StartSplit("SweepSystemWeaks");
392  Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
393  timings_.EndSplit();
394}
395
396struct SweepCallbackContext {
397  SemiSpace* mark_sweep;
398  space::AllocSpace* space;
399  Thread* self;
400};
401
402void SemiSpace::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
403  SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
404  SemiSpace* gc = context->mark_sweep;
405  Heap* heap = gc->GetHeap();
406  space::AllocSpace* space = context->space;
407  Thread* self = context->self;
408  Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
409  size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs);
410  heap->RecordFree(num_ptrs, freed_bytes);
411  gc->freed_objects_.fetch_add(num_ptrs);
412  gc->freed_bytes_.fetch_add(freed_bytes);
413}
414
415void SemiSpace::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
416  SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
417  Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
418  Heap* heap = context->mark_sweep->GetHeap();
419  // We don't free any actual memory to avoid dirtying the shared zygote pages.
420  for (size_t i = 0; i < num_ptrs; ++i) {
421    Object* obj = static_cast<Object*>(ptrs[i]);
422    heap->GetLiveBitmap()->Clear(obj);
423    heap->GetCardTable()->MarkCard(obj);
424  }
425}
426
427void SemiSpace::Sweep(bool swap_bitmaps) {
428  DCHECK(mark_stack_->IsEmpty());
429  TimingLogger::ScopedSplit("Sweep", &timings_);
430
431  const bool partial = (GetGcType() == kGcTypePartial);
432  SweepCallbackContext scc;
433  scc.mark_sweep = this;
434  scc.self = Thread::Current();
435  for (const auto& space : GetHeap()->GetContinuousSpaces()) {
436    if (!space->IsMallocSpace()) {
437      continue;
438    }
439    // We always sweep always collect spaces.
440    bool sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect);
441    if (!partial && !sweep_space) {
442      // We sweep full collect spaces when the GC isn't a partial GC (ie its full).
443      sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect);
444    }
445    if (sweep_space && space->IsMallocSpace()) {
446      uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
447      uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
448      scc.space = space->AsMallocSpace();
449      accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
450      accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
451      if (swap_bitmaps) {
452        std::swap(live_bitmap, mark_bitmap);
453      }
454      if (!space->IsZygoteSpace()) {
455        TimingLogger::ScopedSplit split("SweepAllocSpace", &timings_);
456        // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
457        accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
458                                           &SweepCallback, reinterpret_cast<void*>(&scc));
459      } else {
460        TimingLogger::ScopedSplit split("SweepZygote", &timings_);
461        // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual
462        // memory.
463        accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
464                                           &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
465      }
466    }
467  }
468
469  SweepLargeObjects(swap_bitmaps);
470}
471
472void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
473  TimingLogger::ScopedSplit("SweepLargeObjects", &timings_);
474  // Sweep large objects
475  space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
476  accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
477  accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
478  if (swap_bitmaps) {
479    std::swap(large_live_objects, large_mark_objects);
480  }
481  // O(n*log(n)) but hopefully there are not too many large objects.
482  size_t freed_objects = 0;
483  size_t freed_bytes = 0;
484  Thread* self = Thread::Current();
485  for (const Object* obj : large_live_objects->GetObjects()) {
486    if (!large_mark_objects->Test(obj)) {
487      freed_bytes += large_object_space->Free(self, const_cast<Object*>(obj));
488      ++freed_objects;
489    }
490  }
491  freed_large_objects_.fetch_add(freed_objects);
492  freed_large_object_bytes_.fetch_add(freed_bytes);
493  GetHeap()->RecordFree(freed_objects, freed_bytes);
494}
495
496// Process the "referent" field in a java.lang.ref.Reference.  If the referent has not yet been
497// marked, put it on the appropriate list in the heap for later processing.
498void SemiSpace::DelayReferenceReferent(mirror::Class* klass, Object* obj) {
499  heap_->DelayReferenceReferent(klass, obj, MarkedForwardingAddressCallback, this);
500}
501
502// Visit all of the references of an object and update.
503void SemiSpace::ScanObject(Object* obj) {
504  DCHECK(obj != NULL);
505  DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
506  MarkSweep::VisitObjectReferences(obj, [this](Object* obj, Object* ref, const MemberOffset& offset,
507     bool /* is_static */) ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS {
508    mirror::Object* new_address = MarkObject(ref);
509    if (new_address != ref) {
510      DCHECK(new_address != nullptr);
511      obj->SetFieldObject(offset, new_address, false);
512    }
513  }, kMovingClasses);
514  mirror::Class* klass = obj->GetClass();
515  if (UNLIKELY(klass->IsReferenceClass())) {
516    DelayReferenceReferent(klass, obj);
517  }
518}
519
520// Scan anything that's on the mark stack.
521void SemiSpace::ProcessMarkStack(bool paused) {
522  timings_.StartSplit(paused ? "(paused)ProcessMarkStack" : "ProcessMarkStack");
523  while (!mark_stack_->IsEmpty()) {
524    ScanObject(mark_stack_->PopBack());
525  }
526  timings_.EndSplit();
527}
528
529inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
530    SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
531  // All immune objects are assumed marked.
532  if (IsImmune(obj)) {
533    return obj;
534  }
535  if (from_space_->HasAddress(obj)) {
536    mirror::Object* forwarding_address = GetForwardingAddressInFromSpace(const_cast<Object*>(obj));
537    // If the object is forwarded then it MUST be marked.
538    if (to_space_->HasAddress(forwarding_address)) {
539      return forwarding_address;
540    }
541    // Must not be marked, return nullptr;
542    return nullptr;
543  } else if (to_space_->HasAddress(obj)) {
544    // Already forwarded, must be marked.
545    return obj;
546  }
547  return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
548}
549
550void SemiSpace::UnBindBitmaps() {
551  TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
552  for (const auto& space : GetHeap()->GetContinuousSpaces()) {
553    if (space->IsMallocSpace()) {
554      space::MallocSpace* alloc_space = space->AsMallocSpace();
555      if (alloc_space->HasBoundBitmaps()) {
556        alloc_space->UnBindBitmaps();
557        heap_->GetMarkBitmap()->ReplaceBitmap(alloc_space->GetLiveBitmap(),
558                                              alloc_space->GetMarkBitmap());
559      }
560    }
561  }
562}
563
564void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
565  DCHECK(to_space != nullptr);
566  to_space_ = to_space;
567}
568
569void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
570  DCHECK(from_space != nullptr);
571  from_space_ = from_space;
572}
573
574void SemiSpace::FinishPhase() {
575  TimingLogger::ScopedSplit split("FinishPhase", &timings_);
576  // Can't enqueue references if we hold the mutator lock.
577  Heap* heap = GetHeap();
578  timings_.NewSplit("PostGcVerification");
579  heap->PostGcVerification(this);
580
581  // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
582  // further action is done by the heap.
583  to_space_ = nullptr;
584  from_space_ = nullptr;
585
586  // Update the cumulative statistics
587  total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects();
588  total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes();
589
590  // Ensure that the mark stack is empty.
591  CHECK(mark_stack_->IsEmpty());
592
593  // Update the cumulative loggers.
594  cumulative_timings_.Start();
595  cumulative_timings_.AddLogger(timings_);
596  cumulative_timings_.End();
597
598  // Clear all of the spaces' mark bitmaps.
599  for (const auto& space : GetHeap()->GetContinuousSpaces()) {
600    accounting::SpaceBitmap* bitmap = space->GetMarkBitmap();
601    if (bitmap != nullptr &&
602        space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
603      bitmap->Clear();
604    }
605  }
606  mark_stack_->Reset();
607
608  // Reset the marked large objects.
609  space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
610  large_objects->GetMarkObjects()->Clear();
611}
612
613}  // namespace collector
614}  // namespace gc
615}  // namespace art
616