blob: a4c9deaae6e99fad23ff945bbd253e0085ceb234 [file] [log] [blame]
Mathieu Chartier590fee92013-09-13 13:46:47 -07001/*
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
Mathieu Chartier590fee92013-09-13 13:46:47 -070017#include "semi_space.h"
18
19#include <functional>
20#include <numeric>
21#include <climits>
22#include <vector>
23
24#include "base/logging.h"
25#include "base/macros.h"
26#include "base/mutex-inl.h"
27#include "base/timing_logger.h"
28#include "gc/accounting/heap_bitmap.h"
29#include "gc/accounting/mod_union_table.h"
30#include "gc/accounting/space_bitmap-inl.h"
31#include "gc/heap.h"
32#include "gc/space/bump_pointer_space.h"
33#include "gc/space/bump_pointer_space-inl.h"
34#include "gc/space/image_space.h"
35#include "gc/space/large_object_space.h"
36#include "gc/space/space-inl.h"
37#include "indirect_reference_table.h"
38#include "intern_table.h"
39#include "jni_internal.h"
40#include "mark_sweep-inl.h"
41#include "monitor.h"
42#include "mirror/art_field.h"
43#include "mirror/art_field-inl.h"
44#include "mirror/class-inl.h"
45#include "mirror/class_loader.h"
46#include "mirror/dex_cache.h"
47#include "mirror/object-inl.h"
48#include "mirror/object_array.h"
49#include "mirror/object_array-inl.h"
50#include "runtime.h"
51#include "semi_space-inl.h"
52#include "thread-inl.h"
53#include "thread_list.h"
54#include "verifier/method_verifier.h"
55
56using ::art::mirror::Class;
57using ::art::mirror::Object;
58
59namespace art {
60namespace gc {
61namespace collector {
62
63static constexpr bool kProtectFromSpace = true;
64static constexpr bool kResetFromSpace = true;
65
66// TODO: Unduplicate logic.
67void SemiSpace::ImmuneSpace(space::ContinuousSpace* space) {
68 // Bind live to mark bitmap if necessary.
69 if (space->GetLiveBitmap() != space->GetMarkBitmap()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080070 CHECK(space->IsContinuousMemMapAllocSpace());
71 space->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartier590fee92013-09-13 13:46:47 -070072 }
73 // Add the space to the immune region.
74 if (immune_begin_ == nullptr) {
75 DCHECK(immune_end_ == nullptr);
76 immune_begin_ = reinterpret_cast<Object*>(space->Begin());
77 immune_end_ = reinterpret_cast<Object*>(space->End());
78 } else {
79 const space::ContinuousSpace* prev_space = nullptr;
80 // Find out if the previous space is immune.
81 for (space::ContinuousSpace* cur_space : GetHeap()->GetContinuousSpaces()) {
82 if (cur_space == space) {
83 break;
84 }
85 prev_space = cur_space;
86 }
87 // If previous space was immune, then extend the immune region. Relies on continuous spaces
88 // being sorted by Heap::AddContinuousSpace.
89 if (prev_space != nullptr && IsImmuneSpace(prev_space)) {
90 immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_);
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080091 // Use Limit() instead of End() because otherwise if the
92 // generational mode is enabled, the alloc space might expand
93 // due to promotion and the sense of immunity may change in the
94 // middle of a GC.
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080095 immune_end_ = std::max(reinterpret_cast<Object*>(space->Limit()), immune_end_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070096 }
97 }
98}
99
100void SemiSpace::BindBitmaps() {
101 timings_.StartSplit("BindBitmaps");
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800102 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700103 // Mark all of the spaces we never collect as immune.
104 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800105 if (space->GetLiveBitmap() != nullptr) {
106 if (space == to_space_) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800107 CHECK(to_space_->IsContinuousMemMapAllocSpace());
108 to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800109 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800110 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect
111 // Add the main free list space and the non-moving
112 // space to the immune space if a bump pointer space
113 // only collection.
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800114 || (generational_ && !whole_heap_collection_ &&
115 (space == GetHeap()->GetNonMovingSpace() ||
116 space == GetHeap()->GetPrimaryFreeListSpace()))) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800117 ImmuneSpace(space);
118 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700119 }
120 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800121 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800122 // We won't collect the large object space if a bump pointer space only collection.
123 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800124 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700125 timings_.EndSplit();
126}
127
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800128SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700129 : GarbageCollector(heap,
130 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
131 mark_stack_(nullptr),
132 immune_begin_(nullptr),
133 immune_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800134 is_large_object_space_immune_(false),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700135 to_space_(nullptr),
Ian Rogers6fac4472014-02-25 17:01:10 -0800136 to_space_live_bitmap_(nullptr),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700137 from_space_(nullptr),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800138 self_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800139 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800140 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800141 bytes_promoted_(0),
142 whole_heap_collection_(true),
Ian Rogers6fac4472014-02-25 17:01:10 -0800143 whole_heap_collection_interval_counter_(0),
144 saved_bytes_(0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700145}
146
147void SemiSpace::InitializePhase() {
148 timings_.Reset();
Ian Rogers5fe9af72013-11-14 00:17:20 -0800149 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700150 mark_stack_ = heap_->mark_stack_.get();
151 DCHECK(mark_stack_ != nullptr);
152 immune_begin_ = nullptr;
153 immune_end_ = nullptr;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800154 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800155 saved_bytes_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700156 self_ = Thread::Current();
157 // Do any pre GC verification.
158 timings_.NewSplit("PreGcVerification");
159 heap_->PreGcVerification(this);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800160 // Set the initial bitmap.
161 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700162}
163
164void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800165 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700166 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800167 GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800168 &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700169}
170
171void SemiSpace::MarkingPhase() {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800172 if (generational_) {
173 if (gc_cause_ == kGcCauseExplicit || gc_cause_ == kGcCauseForNativeAlloc ||
174 clear_soft_references_) {
175 // If an explicit, native allocation-triggered, or last attempt
176 // collection, collect the whole heap (and reset the interval
177 // counter to be consistent.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800178 whole_heap_collection_ = true;
179 whole_heap_collection_interval_counter_ = 0;
180 }
181 if (whole_heap_collection_) {
182 VLOG(heap) << "Whole heap collection";
183 } else {
184 VLOG(heap) << "Bump pointer space only collection";
185 }
186 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800187 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800188
Ian Rogers5fe9af72013-11-14 00:17:20 -0800189 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700190 // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the
191 // wrong space.
192 heap_->SwapSemiSpaces();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800193 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800194 // If last_gc_to_space_end_ is out of the bounds of the from-space
195 // (the to-space from last GC), then point it to the beginning of
196 // the from-space. For example, the very first GC or the
197 // pre-zygote compaction.
198 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
199 last_gc_to_space_end_ = from_space_->Begin();
200 }
201 // Reset this before the marking starts below.
202 bytes_promoted_ = 0;
203 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700204 // Assume the cleared space is already empty.
205 BindBitmaps();
206 // Process dirty cards and add dirty cards to mod-union tables.
207 heap_->ProcessCards(timings_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800208 // Clear the whole card table since we can not get any additional dirty cards during the
209 // paused GC. This saves memory but only works for pause the world collectors.
210 timings_.NewSplit("ClearCardTable");
211 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700212 // Need to do this before the checkpoint since we don't want any threads to add references to
213 // the live stack during the recursive mark.
214 timings_.NewSplit("SwapStacks");
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800215 if (kUseThreadLocalAllocationStack) {
216 heap_->RevokeAllThreadLocalAllocationStacks(self_);
217 }
218 heap_->SwapStacks(self_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800219 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700220 MarkRoots();
221 // Mark roots of immune spaces.
222 UpdateAndMarkModUnion();
223 // Recursively mark remaining objects.
224 MarkReachableObjects();
225}
226
227bool SemiSpace::IsImmuneSpace(const space::ContinuousSpace* space) const {
228 return
229 immune_begin_ <= reinterpret_cast<Object*>(space->Begin()) &&
230 immune_end_ >= reinterpret_cast<Object*>(space->End());
231}
232
233void SemiSpace::UpdateAndMarkModUnion() {
234 for (auto& space : heap_->GetContinuousSpaces()) {
235 // If the space is immune then we need to mark the references to other spaces.
236 if (IsImmuneSpace(space)) {
237 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800238 if (table != nullptr) {
239 // TODO: Improve naming.
240 TimingLogger::ScopedSplit split(
241 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
242 "UpdateAndMarkImageModUnionTable",
243 &timings_);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800244 table->UpdateAndMarkReferences(MarkObjectCallback, this);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800245 } else {
246 // If a bump pointer space only collection, the non-moving
247 // space is added to the immune space. But the non-moving
248 // space doesn't have a mod union table. Instead, its live
249 // bitmap will be scanned later in MarkReachableObjects().
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800250 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800251 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()));
252 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700253 }
254 }
255}
256
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800257class SemiSpaceScanObjectVisitor {
258 public:
259 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
260 void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
261 // TODO: fix NO_THREAD_SAFETY_ANALYSIS. ScanObject() requires an
262 // exclusive lock on the mutator lock, but
263 // SpaceBitmap::VisitMarkedRange() only requires the shared lock.
264 DCHECK(obj != nullptr);
265 semi_space_->ScanObject(obj);
266 }
267 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800268 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800269};
270
Mathieu Chartier590fee92013-09-13 13:46:47 -0700271void SemiSpace::MarkReachableObjects() {
272 timings_.StartSplit("MarkStackAsLive");
273 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
274 heap_->MarkAllocStackAsLive(live_stack);
275 live_stack->Reset();
276 timings_.EndSplit();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800277
278 for (auto& space : heap_->GetContinuousSpaces()) {
279 // If the space is immune and has no mod union table (the
280 // non-moving space when the bump pointer space only collection is
281 // enabled,) then we need to scan its live bitmap as roots
282 // (including the objects on the live stack which have just marked
283 // in the live bitmap above in MarkAllocStackAsLive().)
284 if (IsImmuneSpace(space) && heap_->FindModUnionTableFromSpace(space) == nullptr) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800285 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800286 (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace()));
287 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
288 SemiSpaceScanObjectVisitor visitor(this);
289 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
290 reinterpret_cast<uintptr_t>(space->End()),
291 visitor);
292 }
293 }
294
295 if (is_large_object_space_immune_) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800296 DCHECK(generational_ && !whole_heap_collection_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800297 // Delay copying the live set to the marked set until here from
298 // BindBitmaps() as the large objects on the allocation stack may
299 // be newly added to the live set above in MarkAllocStackAsLive().
300 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
301
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800302 // When the large object space is immune, we need to scan the
303 // large object space as roots as they contain references to their
304 // classes (primitive array classes) that could move though they
305 // don't contain any other references.
306 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
307 accounting::ObjectSet* large_live_objects = large_object_space->GetLiveObjects();
308 SemiSpaceScanObjectVisitor visitor(this);
309 for (const Object* obj : large_live_objects->GetObjects()) {
310 visitor(const_cast<Object*>(obj));
311 }
312 }
313
Mathieu Chartier590fee92013-09-13 13:46:47 -0700314 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800315 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700316}
317
318void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800319 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800320 ProcessReferences(self_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700321 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800322 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700323 SweepSystemWeaks();
324 }
325 // Record freed memory.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800326 uint64_t from_bytes = from_space_->GetBytesAllocated();
327 uint64_t to_bytes = to_space_->GetBytesAllocated();
328 uint64_t from_objects = from_space_->GetObjectsAllocated();
329 uint64_t to_objects = to_space_->GetObjectsAllocated();
330 CHECK_LE(to_objects, from_objects);
331 int64_t freed_bytes = from_bytes - to_bytes;
332 int64_t freed_objects = from_objects - to_objects;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800333 freed_bytes_.FetchAndAdd(freed_bytes);
334 freed_objects_.FetchAndAdd(freed_objects);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800335 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
336 // space.
337 heap_->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700338 timings_.StartSplit("PreSweepingGcVerification");
339 heap_->PreSweepingGcVerification(this);
340 timings_.EndSplit();
341
342 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800343 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700344 // Reclaim unmarked objects.
345 Sweep(false);
346 // Swap the live and mark bitmaps for each space which we modified space. This is an
347 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
348 // bitmaps.
349 timings_.StartSplit("SwapBitmaps");
350 SwapBitmaps();
351 timings_.EndSplit();
352 // Unbind the live and mark bitmaps.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800353 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
354 GetHeap()->UnBindBitmaps();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700355 }
356 // Release the memory used by the from space.
357 if (kResetFromSpace) {
358 // Clearing from space.
359 from_space_->Clear();
360 }
361 // Protect the from space.
362 VLOG(heap)
363 << "mprotect region " << reinterpret_cast<void*>(from_space_->Begin()) << " - "
364 << reinterpret_cast<void*>(from_space_->Limit());
365 if (kProtectFromSpace) {
366 mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_NONE);
367 } else {
368 mprotect(from_space_->Begin(), from_space_->Capacity(), PROT_READ);
369 }
Mathieu Chartierad35d902014-02-11 16:20:42 -0800370 if (saved_bytes_ > 0) {
371 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
372 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800373
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800374 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800375 // Record the end (top) of the to space so we can distinguish
376 // between objects that were allocated since the last GC and the
377 // older objects.
378 last_gc_to_space_end_ = to_space_->End();
379 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700380}
381
382void SemiSpace::ResizeMarkStack(size_t new_size) {
383 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
384 CHECK_LE(mark_stack_->Size(), new_size);
385 mark_stack_->Resize(new_size);
386 for (const auto& obj : temp) {
387 mark_stack_->PushBack(obj);
388 }
389}
390
391inline void SemiSpace::MarkStackPush(Object* obj) {
392 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
393 ResizeMarkStack(mark_stack_->Capacity() * 2);
394 }
395 // The object must be pushed on to the mark stack.
396 mark_stack_->PushBack(obj);
397}
398
399// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
400bool SemiSpace::MarkLargeObject(const Object* obj) {
401 // TODO: support >1 discontinuous space.
402 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800403 DCHECK(large_object_space->Contains(obj));
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800404 accounting::ObjectSet* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700405 if (UNLIKELY(!large_objects->Test(obj))) {
406 large_objects->Set(obj);
407 return true;
408 }
409 return false;
410}
411
Mathieu Chartierad35d902014-02-11 16:20:42 -0800412static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
413 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
414 // We will dirty the current page and somewhere in the middle of the next page. This means
415 // that the next object copied will also dirty that page.
416 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
417 // not necessary per GC.
418 memcpy(dest, src, size);
419 return 0;
420 }
421 size_t saved_bytes = 0;
422 byte* byte_dest = reinterpret_cast<byte*>(dest);
423 if (kIsDebugBuild) {
424 for (size_t i = 0; i < size; ++i) {
425 CHECK_EQ(byte_dest[i], 0U);
426 }
427 }
428 // Process the start of the page. The page must already be dirty, don't bother with checking.
429 const byte* byte_src = reinterpret_cast<const byte*>(src);
430 const byte* limit = byte_src + size;
431 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
432 // Copy the bytes until the start of the next page.
433 memcpy(dest, src, page_remain);
434 byte_src += page_remain;
435 byte_dest += page_remain;
436 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
437 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
438 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
439 while (byte_src + kPageSize < limit) {
440 bool all_zero = true;
441 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
442 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
443 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
444 // Assumes the destination of the copy is all zeros.
445 if (word_src[i] != 0) {
446 all_zero = false;
447 word_dest[i] = word_src[i];
448 }
449 }
450 if (all_zero) {
451 // Avoided copying into the page since it was all zeros.
452 saved_bytes += kPageSize;
453 }
454 byte_src += kPageSize;
455 byte_dest += kPageSize;
456 }
457 // Handle the part of the page at the end.
458 memcpy(byte_dest, byte_src, limit - byte_src);
459 return saved_bytes;
460}
461
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800462mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
463 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800464 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800465 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800466 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800467 // If it's allocated before the last GC (older), move
468 // (pseudo-promote) it to the main free list space (as sort
469 // of an old generation.)
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800470 size_t bytes_promoted;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800471 space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
Ian Rogers6fac4472014-02-25 17:01:10 -0800472 forward_address = promo_dest_space->Alloc(self_, object_size, &bytes_promoted, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800473 if (forward_address == nullptr) {
474 // If out of space, fall back to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800475 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800476 } else {
477 GetHeap()->num_bytes_allocated_.FetchAndAdd(bytes_promoted);
478 bytes_promoted_ += bytes_promoted;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800479 // Handle the bitmaps marking.
480 accounting::SpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800481 DCHECK(live_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800482 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800483 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800484 DCHECK(!live_bitmap->Test(forward_address));
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800485 if (!whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800486 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
487 DCHECK_EQ(live_bitmap, mark_bitmap);
488
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800489 // If a bump pointer space only collection, delay the live
490 // bitmap marking of the promoted object until it's popped off
491 // the mark stack (ProcessMarkStack()). The rationale: we may
492 // be in the middle of scanning the objects in the promo
493 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800494 // non-moving-space-to-bump-pointer-space references by
495 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800496 // (MarkReachableObjects()). If we don't delay it (and instead
497 // mark the promoted object here), the above promo destination
498 // space scan could encounter the just-promoted object and
499 // forward the references in the promoted object's fields even
500 // through it is pushed onto the mark stack. If this happens,
501 // the promoted object would be in an inconsistent state, that
502 // is, it's on the mark stack (gray) but its fields are
503 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800504 // DCHECK(!to_space_->HasAddress(obj)) failure below.
505 } else {
506 // Mark forward_address on the live bit map.
507 live_bitmap->Set(forward_address);
508 // Mark forward_address on the mark bit map.
509 DCHECK(!mark_bitmap->Test(forward_address));
510 mark_bitmap->Set(forward_address);
511 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800512 }
513 DCHECK(forward_address != nullptr);
514 } else {
515 // If it's allocated after the last GC (younger), copy it to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800516 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800517 }
518 // Copy over the object and add it to the mark stack since we still need to update its
519 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800520 saved_bytes_ +=
521 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800522 if (kUseBrooksPointer) {
523 obj->AssertSelfBrooksPointer();
524 DCHECK_EQ(forward_address->GetBrooksPointer(), obj);
525 forward_address->SetBrooksPointer(forward_address);
526 forward_address->AssertSelfBrooksPointer();
527 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800528 if (to_space_live_bitmap_ != nullptr) {
529 to_space_live_bitmap_->Set(forward_address);
530 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800531 DCHECK(to_space_->HasAddress(forward_address) ||
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800532 (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800533 return forward_address;
534}
535
Mathieu Chartier590fee92013-09-13 13:46:47 -0700536// Used to mark and copy objects. Any newly-marked objects who are in the from space get moved to
537// the to-space and have their forward address updated. Objects which have been newly marked are
538// pushed on the mark stack.
539Object* SemiSpace::MarkObject(Object* obj) {
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800540 if (kUseBrooksPointer) {
541 // Verify all the objects have the correct forward pointer installed.
542 if (obj != nullptr) {
543 obj->AssertSelfBrooksPointer();
544 }
545 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800546 Object* forward_address = obj;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700547 if (obj != nullptr && !IsImmune(obj)) {
548 if (from_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800549 forward_address = GetForwardingAddressInFromSpace(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700550 // If the object has already been moved, return the new forward address.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800551 if (forward_address == nullptr) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800552 forward_address = MarkNonForwardedObject(obj);
553 DCHECK(forward_address != nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700554 // Make sure to only update the forwarding address AFTER you copy the object so that the
555 // monitor word doesn't get stomped over.
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800556 obj->SetLockWord(LockWord::FromForwardingAddress(
557 reinterpret_cast<size_t>(forward_address)));
558 // Push the object onto the mark stack for later processing.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700559 MarkStackPush(forward_address);
560 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700561 // TODO: Do we need this if in the else statement?
562 } else {
563 accounting::SpaceBitmap* object_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
564 if (LIKELY(object_bitmap != nullptr)) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800565 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800566 // If a bump pointer space only collection, we should not
567 // reach here as we don't/won't mark the objects in the
568 // non-moving space (except for the promoted objects.) Note
569 // the non-moving space is added to the immune space.
570 DCHECK(whole_heap_collection_);
571 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700572 // This object was not previously marked.
573 if (!object_bitmap->Test(obj)) {
574 object_bitmap->Set(obj);
575 MarkStackPush(obj);
576 }
577 } else {
Mathieu Chartierd1e05bf2014-02-04 17:11:58 -0800578 CHECK(!to_space_->HasAddress(obj)) << "Marking object in to_space_";
Mathieu Chartier590fee92013-09-13 13:46:47 -0700579 if (MarkLargeObject(obj)) {
580 MarkStackPush(obj);
581 }
582 }
583 }
584 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800585 return forward_address;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700586}
587
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800588void SemiSpace::ProcessMarkStackCallback(void* arg) {
589 DCHECK(arg != nullptr);
590 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
591}
592
593mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800594 DCHECK(root != nullptr);
595 DCHECK(arg != nullptr);
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800596 return reinterpret_cast<SemiSpace*>(arg)->MarkObject(root);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800597}
598
Mathieu Chartier815873e2014-02-13 18:02:13 -0800599void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
600 RootType /*root_type*/) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700601 DCHECK(root != nullptr);
602 DCHECK(arg != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800603 *root = reinterpret_cast<SemiSpace*>(arg)->MarkObject(*root);
604}
605
Mathieu Chartier590fee92013-09-13 13:46:47 -0700606// Marks all objects in the root set.
607void SemiSpace::MarkRoots() {
608 timings_.StartSplit("MarkRoots");
609 // TODO: Visit up image roots as well?
610 Runtime::Current()->VisitRoots(MarkRootCallback, this, false, true);
611 timings_.EndSplit();
612}
613
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800614mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700615 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
616}
617
618void SemiSpace::SweepSystemWeaks() {
619 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800620 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700621 timings_.EndSplit();
622}
623
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800624bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800625 return space != from_space_ && space != to_space_ && !IsImmuneSpace(space);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700626}
627
628void SemiSpace::Sweep(bool swap_bitmaps) {
629 DCHECK(mark_stack_->IsEmpty());
Ian Rogers5fe9af72013-11-14 00:17:20 -0800630 TimingLogger::ScopedSplit("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700631 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800632 if (space->IsContinuousMemMapAllocSpace()) {
633 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
634 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800635 continue;
636 }
Mathieu Chartierec050072014-01-07 16:00:07 -0800637 TimingLogger::ScopedSplit split(
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800638 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_);
Mathieu Chartierec050072014-01-07 16:00:07 -0800639 size_t freed_objects = 0;
640 size_t freed_bytes = 0;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800641 alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Mathieu Chartierec050072014-01-07 16:00:07 -0800642 heap_->RecordFree(freed_objects, freed_bytes);
643 freed_objects_.FetchAndAdd(freed_objects);
644 freed_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700645 }
646 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800647 if (!is_large_object_space_immune_) {
648 SweepLargeObjects(swap_bitmaps);
649 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650}
651
652void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800653 DCHECK(!is_large_object_space_immune_);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800654 TimingLogger::ScopedSplit("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700655 size_t freed_objects = 0;
656 size_t freed_bytes = 0;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800657 GetHeap()->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800658 freed_large_objects_.FetchAndAdd(freed_objects);
659 freed_large_object_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700660 GetHeap()->RecordFree(freed_objects, freed_bytes);
661}
662
663// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
664// marked, put it on the appropriate list in the heap for later processing.
665void SemiSpace::DelayReferenceReferent(mirror::Class* klass, Object* obj) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800666 heap_->DelayReferenceReferent(klass, obj, MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700667}
668
669// Visit all of the references of an object and update.
670void SemiSpace::ScanObject(Object* obj) {
671 DCHECK(obj != NULL);
672 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
673 MarkSweep::VisitObjectReferences(obj, [this](Object* obj, Object* ref, const MemberOffset& offset,
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100674 bool /* is_static */) ALWAYS_INLINE_LAMBDA NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700675 mirror::Object* new_address = MarkObject(ref);
676 if (new_address != ref) {
677 DCHECK(new_address != nullptr);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800678 // Don't need to mark the card since we updating the object address and not changing the
Ian Rogersef7d42f2014-01-06 12:55:46 -0800679 // actual objects its pointing to. Using SetFieldObjectWithoutWriteBarrier is better in this
680 // case since it does not dirty cards and use additional memory.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100681 // Since we do not change the actual object, we can safely use non-transactional mode. Also
682 // disable check as we could run inside a transaction.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800683 obj->SetFieldObjectWithoutWriteBarrier<false, false, kVerifyNone>(offset, new_address, false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700684 }
685 }, kMovingClasses);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800686 mirror::Class* klass = obj->GetClass<kVerifyNone>();
687 if (UNLIKELY(klass->IsReferenceClass<kVerifyNone>())) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700688 DelayReferenceReferent(klass, obj);
689 }
690}
691
692// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800693void SemiSpace::ProcessMarkStack() {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800694 space::MallocSpace* promo_dest_space = NULL;
695 accounting::SpaceBitmap* live_bitmap = NULL;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800696 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800697 // If a bump pointer space only collection (and the promotion is
698 // enabled,) we delay the live-bitmap marking of promoted objects
699 // from MarkObject() until this function.
700 promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
701 live_bitmap = promo_dest_space->GetLiveBitmap();
702 DCHECK(live_bitmap != nullptr);
703 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
704 DCHECK(mark_bitmap != nullptr);
705 DCHECK_EQ(live_bitmap, mark_bitmap);
706 }
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800707 timings_.StartSplit("ProcessMarkStack");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700708 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800709 Object* obj = mark_stack_->PopBack();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800710 if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800711 // obj has just been promoted. Mark the live bitmap for it,
712 // which is delayed from MarkObject().
713 DCHECK(!live_bitmap->Test(obj));
714 live_bitmap->Set(obj);
715 }
716 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700717 }
718 timings_.EndSplit();
719}
720
Mathieu Chartier590fee92013-09-13 13:46:47 -0700721inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
722 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
723 // All immune objects are assumed marked.
724 if (IsImmune(obj)) {
725 return obj;
726 }
727 if (from_space_->HasAddress(obj)) {
728 mirror::Object* forwarding_address = GetForwardingAddressInFromSpace(const_cast<Object*>(obj));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800729 return forwarding_address; // Returns either the forwarding address or nullptr.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700730 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800731 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700732 // Already forwarded, must be marked.
733 return obj;
734 }
735 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
736}
737
Mathieu Chartier590fee92013-09-13 13:46:47 -0700738void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
739 DCHECK(to_space != nullptr);
740 to_space_ = to_space;
741}
742
743void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
744 DCHECK(from_space != nullptr);
745 from_space_ = from_space;
746}
747
748void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800749 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700750 Heap* heap = GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700751 timings_.NewSplit("PostGcVerification");
752 heap->PostGcVerification(this);
753
754 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
755 // further action is done by the heap.
756 to_space_ = nullptr;
757 from_space_ = nullptr;
758
759 // Update the cumulative statistics
Mathieu Chartier590fee92013-09-13 13:46:47 -0700760 total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects();
761 total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes();
762
763 // Ensure that the mark stack is empty.
764 CHECK(mark_stack_->IsEmpty());
765
766 // Update the cumulative loggers.
767 cumulative_timings_.Start();
768 cumulative_timings_.AddLogger(timings_);
769 cumulative_timings_.End();
770
771 // Clear all of the spaces' mark bitmaps.
772 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
773 accounting::SpaceBitmap* bitmap = space->GetMarkBitmap();
774 if (bitmap != nullptr &&
775 space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
776 bitmap->Clear();
777 }
778 }
779 mark_stack_->Reset();
780
781 // Reset the marked large objects.
782 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
783 large_objects->GetMarkObjects()->Clear();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800784
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800785 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800786 // Decide whether to do a whole heap collection or a bump pointer
787 // only space collection at the next collection by updating
788 // whole_heap_collection. Enable whole_heap_collection once every
789 // kDefaultWholeHeapCollectionInterval collections.
790 if (!whole_heap_collection_) {
791 --whole_heap_collection_interval_counter_;
792 DCHECK_GE(whole_heap_collection_interval_counter_, 0);
793 if (whole_heap_collection_interval_counter_ == 0) {
794 whole_heap_collection_ = true;
795 }
796 } else {
797 DCHECK_EQ(whole_heap_collection_interval_counter_, 0);
798 whole_heap_collection_interval_counter_ = kDefaultWholeHeapCollectionInterval;
799 whole_heap_collection_ = false;
800 }
801 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700802}
803
804} // namespace collector
805} // namespace gc
806} // namespace art