blob: 23b155cffcb8e6de52f8bce5fd6522c92157639a [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"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070047#include "mirror/reference-inl.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070048#include "mirror/object-inl.h"
49#include "mirror/object_array.h"
50#include "mirror/object_array-inl.h"
51#include "runtime.h"
52#include "semi_space-inl.h"
53#include "thread-inl.h"
54#include "thread_list.h"
55#include "verifier/method_verifier.h"
56
57using ::art::mirror::Class;
58using ::art::mirror::Object;
59
60namespace art {
61namespace gc {
62namespace collector {
63
64static constexpr bool kProtectFromSpace = true;
Mathieu Chartier15d34022014-02-26 17:16:38 -080065static constexpr bool kClearFromSpace = true;
66static constexpr bool kStoreStackTraces = false;
Mathieu Chartier590fee92013-09-13 13:46:47 -070067
Mathieu Chartier590fee92013-09-13 13:46:47 -070068void SemiSpace::BindBitmaps() {
69 timings_.StartSplit("BindBitmaps");
Mathieu Chartiera1602f22014-01-13 17:19:19 -080070 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070071 // Mark all of the spaces we never collect as immune.
72 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080073 if (space->GetLiveBitmap() != nullptr) {
74 if (space == to_space_) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080075 CHECK(to_space_->IsContinuousMemMapAllocSpace());
76 to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080077 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080078 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect
79 // Add the main free list space and the non-moving
80 // space to the immune space if a bump pointer space
81 // only collection.
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080082 || (generational_ && !whole_heap_collection_ &&
83 (space == GetHeap()->GetNonMovingSpace() ||
84 space == GetHeap()->GetPrimaryFreeListSpace()))) {
Mathieu Chartier8d562102014-03-12 17:42:10 -070085 CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080086 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070087 }
88 }
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080089 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080090 // We won't collect the large object space if a bump pointer space only collection.
91 is_large_object_space_immune_ = true;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -080092 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070093 timings_.EndSplit();
94}
95
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -080096SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix)
Mathieu Chartier590fee92013-09-13 13:46:47 -070097 : GarbageCollector(heap,
98 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
99 mark_stack_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800100 is_large_object_space_immune_(false),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700101 to_space_(nullptr),
Ian Rogers6fac4472014-02-25 17:01:10 -0800102 to_space_live_bitmap_(nullptr),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700103 from_space_(nullptr),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800104 self_(nullptr),
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800105 generational_(generational),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800106 last_gc_to_space_end_(nullptr),
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800107 bytes_promoted_(0),
108 whole_heap_collection_(true),
Ian Rogers6fac4472014-02-25 17:01:10 -0800109 whole_heap_collection_interval_counter_(0),
110 saved_bytes_(0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700111}
112
113void SemiSpace::InitializePhase() {
114 timings_.Reset();
Ian Rogers5fe9af72013-11-14 00:17:20 -0800115 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700116 mark_stack_ = heap_->mark_stack_.get();
117 DCHECK(mark_stack_ != nullptr);
Mathieu Chartier8d562102014-03-12 17:42:10 -0700118 immune_region_.Reset();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800119 is_large_object_space_immune_ = false;
Mathieu Chartierad35d902014-02-11 16:20:42 -0800120 saved_bytes_ = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700121 self_ = Thread::Current();
122 // Do any pre GC verification.
123 timings_.NewSplit("PreGcVerification");
124 heap_->PreGcVerification(this);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800125 // Set the initial bitmap.
126 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700127}
128
129void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800130 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700131 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800132 GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800133 &MarkObjectCallback, &ProcessMarkStackCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700134}
135
136void SemiSpace::MarkingPhase() {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800137 if (kStoreStackTraces) {
138 Locks::mutator_lock_->AssertExclusiveHeld(self_);
139 // Store the stack traces into the runtime fault string in case we get a heap corruption
140 // related crash later.
141 ThreadState old_state = self_->SetStateUnsafe(kRunnable);
142 std::ostringstream oss;
143 Runtime* runtime = Runtime::Current();
144 runtime->GetThreadList()->DumpForSigQuit(oss);
145 runtime->GetThreadList()->DumpNativeStacks(oss);
146 runtime->SetFaultMessage(oss.str());
147 CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable);
148 }
149
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800150 if (generational_) {
151 if (gc_cause_ == kGcCauseExplicit || gc_cause_ == kGcCauseForNativeAlloc ||
152 clear_soft_references_) {
153 // If an explicit, native allocation-triggered, or last attempt
154 // collection, collect the whole heap (and reset the interval
155 // counter to be consistent.)
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800156 whole_heap_collection_ = true;
157 whole_heap_collection_interval_counter_ = 0;
158 }
159 if (whole_heap_collection_) {
160 VLOG(heap) << "Whole heap collection";
161 } else {
162 VLOG(heap) << "Bump pointer space only collection";
163 }
164 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800165 Locks::mutator_lock_->AssertExclusiveHeld(self_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800166
Ian Rogers5fe9af72013-11-14 00:17:20 -0800167 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700168 // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the
169 // wrong space.
170 heap_->SwapSemiSpaces();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800171 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800172 // If last_gc_to_space_end_ is out of the bounds of the from-space
173 // (the to-space from last GC), then point it to the beginning of
174 // the from-space. For example, the very first GC or the
175 // pre-zygote compaction.
176 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
177 last_gc_to_space_end_ = from_space_->Begin();
178 }
179 // Reset this before the marking starts below.
180 bytes_promoted_ = 0;
181 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700182 // Assume the cleared space is already empty.
183 BindBitmaps();
184 // Process dirty cards and add dirty cards to mod-union tables.
185 heap_->ProcessCards(timings_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800186 // Clear the whole card table since we can not get any additional dirty cards during the
187 // paused GC. This saves memory but only works for pause the world collectors.
188 timings_.NewSplit("ClearCardTable");
189 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700190 // Need to do this before the checkpoint since we don't want any threads to add references to
191 // the live stack during the recursive mark.
192 timings_.NewSplit("SwapStacks");
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -0800193 if (kUseThreadLocalAllocationStack) {
194 heap_->RevokeAllThreadLocalAllocationStacks(self_);
195 }
196 heap_->SwapStacks(self_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800197 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700198 MarkRoots();
199 // Mark roots of immune spaces.
200 UpdateAndMarkModUnion();
201 // Recursively mark remaining objects.
202 MarkReachableObjects();
203}
204
Mathieu Chartier590fee92013-09-13 13:46:47 -0700205void SemiSpace::UpdateAndMarkModUnion() {
206 for (auto& space : heap_->GetContinuousSpaces()) {
207 // If the space is immune then we need to mark the references to other spaces.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700208 if (immune_region_.ContainsSpace(space)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700209 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800210 if (table != nullptr) {
211 // TODO: Improve naming.
212 TimingLogger::ScopedSplit split(
213 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
214 "UpdateAndMarkImageModUnionTable",
215 &timings_);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800216 table->UpdateAndMarkReferences(MarkObjectCallback, this);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800217 } else {
218 // If a bump pointer space only collection, the non-moving
219 // space is added to the immune space. But the non-moving
220 // space doesn't have a mod union table. Instead, its live
221 // bitmap will be scanned later in MarkReachableObjects().
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800222 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800223 (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace()));
224 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700225 }
226 }
227}
228
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800229class SemiSpaceScanObjectVisitor {
230 public:
231 explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {}
232 void operator()(Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
233 // TODO: fix NO_THREAD_SAFETY_ANALYSIS. ScanObject() requires an
234 // exclusive lock on the mutator lock, but
235 // SpaceBitmap::VisitMarkedRange() only requires the shared lock.
236 DCHECK(obj != nullptr);
237 semi_space_->ScanObject(obj);
238 }
239 private:
Ian Rogers6fac4472014-02-25 17:01:10 -0800240 SemiSpace* const semi_space_;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800241};
242
Mathieu Chartier590fee92013-09-13 13:46:47 -0700243void SemiSpace::MarkReachableObjects() {
244 timings_.StartSplit("MarkStackAsLive");
245 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
246 heap_->MarkAllocStackAsLive(live_stack);
247 live_stack->Reset();
248 timings_.EndSplit();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800249
250 for (auto& space : heap_->GetContinuousSpaces()) {
251 // If the space is immune and has no mod union table (the
252 // non-moving space when the bump pointer space only collection is
253 // enabled,) then we need to scan its live bitmap as roots
254 // (including the objects on the live stack which have just marked
255 // in the live bitmap above in MarkAllocStackAsLive().)
Mathieu Chartier8d562102014-03-12 17:42:10 -0700256 if (immune_region_.ContainsSpace(space) &&
257 heap_->FindModUnionTableFromSpace(space) == nullptr) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800258 DCHECK(generational_ && !whole_heap_collection_ &&
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800259 (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace()));
260 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
261 SemiSpaceScanObjectVisitor visitor(this);
262 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()),
263 reinterpret_cast<uintptr_t>(space->End()),
264 visitor);
265 }
266 }
267
268 if (is_large_object_space_immune_) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800269 DCHECK(generational_ && !whole_heap_collection_);
Hiroshi Yamauchiba5870d2014-01-29 15:31:03 -0800270 // Delay copying the live set to the marked set until here from
271 // BindBitmaps() as the large objects on the allocation stack may
272 // be newly added to the live set above in MarkAllocStackAsLive().
273 GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked();
274
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800275 // When the large object space is immune, we need to scan the
276 // large object space as roots as they contain references to their
277 // classes (primitive array classes) that could move though they
278 // don't contain any other references.
279 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
280 accounting::ObjectSet* large_live_objects = large_object_space->GetLiveObjects();
281 SemiSpaceScanObjectVisitor visitor(this);
282 for (const Object* obj : large_live_objects->GetObjects()) {
283 visitor(const_cast<Object*>(obj));
284 }
285 }
286
Mathieu Chartier590fee92013-09-13 13:46:47 -0700287 // Recursively process the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800288 ProcessMarkStack();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700289}
290
291void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800292 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800293 ProcessReferences(self_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700294 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800295 ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700296 SweepSystemWeaks();
297 }
298 // Record freed memory.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800299 uint64_t from_bytes = from_space_->GetBytesAllocated();
300 uint64_t to_bytes = to_space_->GetBytesAllocated();
301 uint64_t from_objects = from_space_->GetObjectsAllocated();
302 uint64_t to_objects = to_space_->GetObjectsAllocated();
303 CHECK_LE(to_objects, from_objects);
304 int64_t freed_bytes = from_bytes - to_bytes;
305 int64_t freed_objects = from_objects - to_objects;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800306 freed_bytes_.FetchAndAdd(freed_bytes);
307 freed_objects_.FetchAndAdd(freed_objects);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800308 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
309 // space.
310 heap_->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700311 timings_.StartSplit("PreSweepingGcVerification");
312 heap_->PreSweepingGcVerification(this);
313 timings_.EndSplit();
314
315 {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800316 WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700317 // Reclaim unmarked objects.
318 Sweep(false);
319 // Swap the live and mark bitmaps for each space which we modified space. This is an
320 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
321 // bitmaps.
322 timings_.StartSplit("SwapBitmaps");
323 SwapBitmaps();
324 timings_.EndSplit();
325 // Unbind the live and mark bitmaps.
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800326 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
327 GetHeap()->UnBindBitmaps();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700328 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800329 if (kClearFromSpace) {
330 // Release the memory used by the from space.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700331 from_space_->Clear();
332 }
Mathieu Chartier15d34022014-02-26 17:16:38 -0800333 from_space_->Reset();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700334 // Protect the from space.
Mathieu Chartier15d34022014-02-26 17:16:38 -0800335 VLOG(heap) << "Protecting space " << *from_space_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700336 if (kProtectFromSpace) {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800337 from_space_->GetMemMap()->Protect(PROT_NONE);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700338 } else {
Mathieu Chartier15d34022014-02-26 17:16:38 -0800339 from_space_->GetMemMap()->Protect(PROT_READ);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700340 }
Mathieu Chartierad35d902014-02-11 16:20:42 -0800341 if (saved_bytes_ > 0) {
342 VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_);
343 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800344
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800345 if (generational_) {
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800346 // Record the end (top) of the to space so we can distinguish
347 // between objects that were allocated since the last GC and the
348 // older objects.
349 last_gc_to_space_end_ = to_space_->End();
350 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700351}
352
353void SemiSpace::ResizeMarkStack(size_t new_size) {
354 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
355 CHECK_LE(mark_stack_->Size(), new_size);
356 mark_stack_->Resize(new_size);
357 for (const auto& obj : temp) {
358 mark_stack_->PushBack(obj);
359 }
360}
361
362inline void SemiSpace::MarkStackPush(Object* obj) {
363 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
364 ResizeMarkStack(mark_stack_->Capacity() * 2);
365 }
366 // The object must be pushed on to the mark stack.
367 mark_stack_->PushBack(obj);
368}
369
370// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
371bool SemiSpace::MarkLargeObject(const Object* obj) {
372 // TODO: support >1 discontinuous space.
373 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800374 DCHECK(large_object_space->Contains(obj));
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800375 accounting::ObjectSet* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700376 if (UNLIKELY(!large_objects->Test(obj))) {
377 large_objects->Set(obj);
378 return true;
379 }
380 return false;
381}
382
Mathieu Chartierad35d902014-02-11 16:20:42 -0800383static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) {
384 if (LIKELY(size <= static_cast<size_t>(kPageSize))) {
385 // We will dirty the current page and somewhere in the middle of the next page. This means
386 // that the next object copied will also dirty that page.
387 // TODO: Worth considering the last object copied? We may end up dirtying one page which is
388 // not necessary per GC.
389 memcpy(dest, src, size);
390 return 0;
391 }
392 size_t saved_bytes = 0;
393 byte* byte_dest = reinterpret_cast<byte*>(dest);
394 if (kIsDebugBuild) {
395 for (size_t i = 0; i < size; ++i) {
396 CHECK_EQ(byte_dest[i], 0U);
397 }
398 }
399 // Process the start of the page. The page must already be dirty, don't bother with checking.
400 const byte* byte_src = reinterpret_cast<const byte*>(src);
401 const byte* limit = byte_src + size;
402 size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest;
403 // Copy the bytes until the start of the next page.
404 memcpy(dest, src, page_remain);
405 byte_src += page_remain;
406 byte_dest += page_remain;
407 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize);
408 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t));
409 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t));
410 while (byte_src + kPageSize < limit) {
411 bool all_zero = true;
412 uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest);
413 const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src);
414 for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) {
415 // Assumes the destination of the copy is all zeros.
416 if (word_src[i] != 0) {
417 all_zero = false;
418 word_dest[i] = word_src[i];
419 }
420 }
421 if (all_zero) {
422 // Avoided copying into the page since it was all zeros.
423 saved_bytes += kPageSize;
424 }
425 byte_src += kPageSize;
426 byte_dest += kPageSize;
427 }
428 // Handle the part of the page at the end.
429 memcpy(byte_dest, byte_src, limit - byte_src);
430 return saved_bytes;
431}
432
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800433mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
434 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800435 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800436 mirror::Object* forward_address = nullptr;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800437 if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800438 // If it's allocated before the last GC (older), move
439 // (pseudo-promote) it to the main free list space (as sort
440 // of an old generation.)
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800441 size_t bytes_promoted;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800442 space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
Ian Rogers6fac4472014-02-25 17:01:10 -0800443 forward_address = promo_dest_space->Alloc(self_, object_size, &bytes_promoted, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800444 if (forward_address == nullptr) {
445 // If out of space, fall back to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800446 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800447 } else {
448 GetHeap()->num_bytes_allocated_.FetchAndAdd(bytes_promoted);
449 bytes_promoted_ += bytes_promoted;
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800450 // Handle the bitmaps marking.
451 accounting::SpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800452 DCHECK(live_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800453 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800454 DCHECK(mark_bitmap != nullptr);
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800455 DCHECK(!live_bitmap->Test(forward_address));
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800456 if (!whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800457 // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap.
458 DCHECK_EQ(live_bitmap, mark_bitmap);
459
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800460 // If a bump pointer space only collection, delay the live
461 // bitmap marking of the promoted object until it's popped off
462 // the mark stack (ProcessMarkStack()). The rationale: we may
463 // be in the middle of scanning the objects in the promo
464 // destination space for
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800465 // non-moving-space-to-bump-pointer-space references by
466 // iterating over the marked bits of the live bitmap
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800467 // (MarkReachableObjects()). If we don't delay it (and instead
468 // mark the promoted object here), the above promo destination
469 // space scan could encounter the just-promoted object and
470 // forward the references in the promoted object's fields even
471 // through it is pushed onto the mark stack. If this happens,
472 // the promoted object would be in an inconsistent state, that
473 // is, it's on the mark stack (gray) but its fields are
474 // already forwarded (black), which would cause a
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800475 // DCHECK(!to_space_->HasAddress(obj)) failure below.
476 } else {
477 // Mark forward_address on the live bit map.
478 live_bitmap->Set(forward_address);
479 // Mark forward_address on the mark bit map.
480 DCHECK(!mark_bitmap->Test(forward_address));
481 mark_bitmap->Set(forward_address);
482 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800483 }
484 DCHECK(forward_address != nullptr);
485 } else {
486 // If it's allocated after the last GC (younger), copy it to the to-space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800487 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated, nullptr);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800488 }
489 // Copy over the object and add it to the mark stack since we still need to update its
490 // references.
Mathieu Chartierad35d902014-02-11 16:20:42 -0800491 saved_bytes_ +=
492 CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size);
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800493 if (kUseBrooksPointer) {
494 obj->AssertSelfBrooksPointer();
495 DCHECK_EQ(forward_address->GetBrooksPointer(), obj);
496 forward_address->SetBrooksPointer(forward_address);
497 forward_address->AssertSelfBrooksPointer();
498 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800499 if (to_space_live_bitmap_ != nullptr) {
500 to_space_live_bitmap_->Set(forward_address);
501 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800502 DCHECK(to_space_->HasAddress(forward_address) ||
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800503 (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800504 return forward_address;
505}
506
Mathieu Chartier590fee92013-09-13 13:46:47 -0700507// Used to mark and copy objects. Any newly-marked objects who are in the from space get moved to
508// the to-space and have their forward address updated. Objects which have been newly marked are
509// pushed on the mark stack.
510Object* SemiSpace::MarkObject(Object* obj) {
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800511 if (kUseBrooksPointer) {
512 // Verify all the objects have the correct forward pointer installed.
513 if (obj != nullptr) {
514 obj->AssertSelfBrooksPointer();
515 }
516 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800517 Object* forward_address = obj;
Mathieu Chartier8d562102014-03-12 17:42:10 -0700518 if (obj != nullptr && !immune_region_.ContainsObject(obj)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700519 if (from_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800520 forward_address = GetForwardingAddressInFromSpace(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700521 // If the object has already been moved, return the new forward address.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800522 if (forward_address == nullptr) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800523 forward_address = MarkNonForwardedObject(obj);
524 DCHECK(forward_address != nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700525 // Make sure to only update the forwarding address AFTER you copy the object so that the
526 // monitor word doesn't get stomped over.
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800527 obj->SetLockWord(LockWord::FromForwardingAddress(
528 reinterpret_cast<size_t>(forward_address)));
529 // Push the object onto the mark stack for later processing.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700530 MarkStackPush(forward_address);
531 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700532 // TODO: Do we need this if in the else statement?
533 } else {
534 accounting::SpaceBitmap* object_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
535 if (LIKELY(object_bitmap != nullptr)) {
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800536 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800537 // If a bump pointer space only collection, we should not
538 // reach here as we don't/won't mark the objects in the
539 // non-moving space (except for the promoted objects.) Note
540 // the non-moving space is added to the immune space.
541 DCHECK(whole_heap_collection_);
542 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700543 // This object was not previously marked.
544 if (!object_bitmap->Test(obj)) {
545 object_bitmap->Set(obj);
546 MarkStackPush(obj);
547 }
548 } else {
Mathieu Chartierd1e05bf2014-02-04 17:11:58 -0800549 CHECK(!to_space_->HasAddress(obj)) << "Marking object in to_space_";
Mathieu Chartier590fee92013-09-13 13:46:47 -0700550 if (MarkLargeObject(obj)) {
551 MarkStackPush(obj);
552 }
553 }
554 }
555 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800556 return forward_address;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700557}
558
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800559void SemiSpace::ProcessMarkStackCallback(void* arg) {
560 DCHECK(arg != nullptr);
561 reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack();
562}
563
564mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800565 DCHECK(root != nullptr);
566 DCHECK(arg != nullptr);
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800567 return reinterpret_cast<SemiSpace*>(arg)->MarkObject(root);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800568}
569
Mathieu Chartier815873e2014-02-13 18:02:13 -0800570void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/,
571 RootType /*root_type*/) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700572 DCHECK(root != nullptr);
573 DCHECK(arg != nullptr);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800574 *root = reinterpret_cast<SemiSpace*>(arg)->MarkObject(*root);
575}
576
Mathieu Chartier590fee92013-09-13 13:46:47 -0700577// Marks all objects in the root set.
578void SemiSpace::MarkRoots() {
579 timings_.StartSplit("MarkRoots");
580 // TODO: Visit up image roots as well?
Mathieu Chartier893263b2014-03-04 11:07:42 -0800581 Runtime::Current()->VisitRoots(MarkRootCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700582 timings_.EndSplit();
583}
584
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800585mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700586 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
587}
588
589void SemiSpace::SweepSystemWeaks() {
590 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800591 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700592 timings_.EndSplit();
593}
594
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800595bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const {
Mathieu Chartier8d562102014-03-12 17:42:10 -0700596 return space != from_space_ && space != to_space_ && !immune_region_.ContainsSpace(space);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700597}
598
599void SemiSpace::Sweep(bool swap_bitmaps) {
600 DCHECK(mark_stack_->IsEmpty());
Ian Rogers5fe9af72013-11-14 00:17:20 -0800601 TimingLogger::ScopedSplit("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700602 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800603 if (space->IsContinuousMemMapAllocSpace()) {
604 space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace();
605 if (!ShouldSweepSpace(alloc_space)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800606 continue;
607 }
Mathieu Chartierec050072014-01-07 16:00:07 -0800608 TimingLogger::ScopedSplit split(
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800609 alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_);
Mathieu Chartierec050072014-01-07 16:00:07 -0800610 size_t freed_objects = 0;
611 size_t freed_bytes = 0;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800612 alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Mathieu Chartierec050072014-01-07 16:00:07 -0800613 heap_->RecordFree(freed_objects, freed_bytes);
614 freed_objects_.FetchAndAdd(freed_objects);
615 freed_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700616 }
617 }
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800618 if (!is_large_object_space_immune_) {
619 SweepLargeObjects(swap_bitmaps);
620 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700621}
622
623void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800624 DCHECK(!is_large_object_space_immune_);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800625 TimingLogger::ScopedSplit("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700626 size_t freed_objects = 0;
627 size_t freed_bytes = 0;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800628 GetHeap()->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800629 freed_large_objects_.FetchAndAdd(freed_objects);
630 freed_large_object_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700631 GetHeap()->RecordFree(freed_objects, freed_bytes);
632}
633
634// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
635// marked, put it on the appropriate list in the heap for later processing.
636void SemiSpace::DelayReferenceReferent(mirror::Class* klass, Object* obj) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700637 heap_->DelayReferenceReferent(klass, obj->AsReference(), MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700638}
639
Ian Rogers719d1a32014-03-06 12:13:39 -0800640class SemiSpaceMarkObjectVisitor {
641 public:
642 explicit SemiSpaceMarkObjectVisitor(SemiSpace* semi_space) : semi_space_(semi_space) {
643 }
644
645 void operator()(Object* obj, Object* ref, const MemberOffset& offset, bool /* is_static */)
646 const ALWAYS_INLINE NO_THREAD_SAFETY_ANALYSIS /* EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) */ {
647 mirror::Object* new_address = semi_space_->MarkObject(ref);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700648 if (new_address != ref) {
649 DCHECK(new_address != nullptr);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800650 // Don't need to mark the card since we updating the object address and not changing the
Ian Rogersef7d42f2014-01-06 12:55:46 -0800651 // actual objects its pointing to. Using SetFieldObjectWithoutWriteBarrier is better in this
652 // case since it does not dirty cards and use additional memory.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100653 // Since we do not change the actual object, we can safely use non-transactional mode. Also
654 // disable check as we could run inside a transaction.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800655 obj->SetFieldObjectWithoutWriteBarrier<false, false, kVerifyNone>(offset, new_address, false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700656 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800657 }
658 private:
659 SemiSpace* const semi_space_;
660};
661
662// Visit all of the references of an object and update.
663void SemiSpace::ScanObject(Object* obj) {
664 DCHECK(obj != NULL);
665 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
666 SemiSpaceMarkObjectVisitor visitor(this);
667 MarkSweep::VisitObjectReferences(obj, visitor, kMovingClasses);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800668 mirror::Class* klass = obj->GetClass<kVerifyNone>();
669 if (UNLIKELY(klass->IsReferenceClass<kVerifyNone>())) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700670 DelayReferenceReferent(klass, obj);
671 }
672}
673
674// Scan anything that's on the mark stack.
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800675void SemiSpace::ProcessMarkStack() {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800676 space::MallocSpace* promo_dest_space = NULL;
677 accounting::SpaceBitmap* live_bitmap = NULL;
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800678 if (generational_ && !whole_heap_collection_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800679 // If a bump pointer space only collection (and the promotion is
680 // enabled,) we delay the live-bitmap marking of promoted objects
681 // from MarkObject() until this function.
682 promo_dest_space = GetHeap()->GetPrimaryFreeListSpace();
683 live_bitmap = promo_dest_space->GetLiveBitmap();
684 DCHECK(live_bitmap != nullptr);
685 accounting::SpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap();
686 DCHECK(mark_bitmap != nullptr);
687 DCHECK_EQ(live_bitmap, mark_bitmap);
688 }
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800689 timings_.StartSplit("ProcessMarkStack");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700690 while (!mark_stack_->IsEmpty()) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800691 Object* obj = mark_stack_->PopBack();
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800692 if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800693 // obj has just been promoted. Mark the live bitmap for it,
694 // which is delayed from MarkObject().
695 DCHECK(!live_bitmap->Test(obj));
696 live_bitmap->Set(obj);
697 }
698 ScanObject(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700699 }
700 timings_.EndSplit();
701}
702
Mathieu Chartier590fee92013-09-13 13:46:47 -0700703inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
704 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
705 // All immune objects are assumed marked.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700706 if (immune_region_.ContainsObject(obj)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700707 return obj;
708 }
709 if (from_space_->HasAddress(obj)) {
710 mirror::Object* forwarding_address = GetForwardingAddressInFromSpace(const_cast<Object*>(obj));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800711 return forwarding_address; // Returns either the forwarding address or nullptr.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700712 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800713 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700714 // Already forwarded, must be marked.
715 return obj;
716 }
717 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
718}
719
Mathieu Chartier590fee92013-09-13 13:46:47 -0700720void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
721 DCHECK(to_space != nullptr);
722 to_space_ = to_space;
723}
724
725void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
726 DCHECK(from_space != nullptr);
727 from_space_ = from_space;
728}
729
730void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800731 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700732 Heap* heap = GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700733 timings_.NewSplit("PostGcVerification");
734 heap->PostGcVerification(this);
735
736 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
737 // further action is done by the heap.
738 to_space_ = nullptr;
739 from_space_ = nullptr;
740
741 // Update the cumulative statistics
Mathieu Chartier590fee92013-09-13 13:46:47 -0700742 total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects();
743 total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes();
744
745 // Ensure that the mark stack is empty.
746 CHECK(mark_stack_->IsEmpty());
747
748 // Update the cumulative loggers.
749 cumulative_timings_.Start();
750 cumulative_timings_.AddLogger(timings_);
751 cumulative_timings_.End();
752
753 // Clear all of the spaces' mark bitmaps.
754 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
755 accounting::SpaceBitmap* bitmap = space->GetMarkBitmap();
756 if (bitmap != nullptr &&
757 space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
758 bitmap->Clear();
759 }
760 }
761 mark_stack_->Reset();
762
763 // Reset the marked large objects.
764 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
765 large_objects->GetMarkObjects()->Clear();
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800766
Hiroshi Yamauchi6f4ffe42014-01-13 12:30:44 -0800767 if (generational_) {
Hiroshi Yamauchi05e713a2014-01-09 13:24:51 -0800768 // Decide whether to do a whole heap collection or a bump pointer
769 // only space collection at the next collection by updating
770 // whole_heap_collection. Enable whole_heap_collection once every
771 // kDefaultWholeHeapCollectionInterval collections.
772 if (!whole_heap_collection_) {
773 --whole_heap_collection_interval_counter_;
774 DCHECK_GE(whole_heap_collection_interval_counter_, 0);
775 if (whole_heap_collection_interval_counter_ == 0) {
776 whole_heap_collection_ = true;
777 }
778 } else {
779 DCHECK_EQ(whole_heap_collection_interval_counter_, 0);
780 whole_heap_collection_interval_counter_ = kDefaultWholeHeapCollectionInterval;
781 whole_heap_collection_ = false;
782 }
783 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700784}
785
786} // namespace collector
787} // namespace gc
788} // namespace art