blob: 1ea200236ee13b61b25dce6e54c65b8bcbe2e4b2 [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;
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -080065// TODO: move this to a new file as a new garbage collector?
66static constexpr bool kEnableSimplePromo = false;
Mathieu Chartier590fee92013-09-13 13:46:47 -070067
68// TODO: Unduplicate logic.
69void SemiSpace::ImmuneSpace(space::ContinuousSpace* space) {
70 // Bind live to mark bitmap if necessary.
71 if (space->GetLiveBitmap() != space->GetMarkBitmap()) {
72 BindLiveToMarkBitmap(space);
73 }
74 // Add the space to the immune region.
75 if (immune_begin_ == nullptr) {
76 DCHECK(immune_end_ == nullptr);
77 immune_begin_ = reinterpret_cast<Object*>(space->Begin());
78 immune_end_ = reinterpret_cast<Object*>(space->End());
79 } else {
80 const space::ContinuousSpace* prev_space = nullptr;
81 // Find out if the previous space is immune.
82 for (space::ContinuousSpace* cur_space : GetHeap()->GetContinuousSpaces()) {
83 if (cur_space == space) {
84 break;
85 }
86 prev_space = cur_space;
87 }
88 // If previous space was immune, then extend the immune region. Relies on continuous spaces
89 // being sorted by Heap::AddContinuousSpace.
90 if (prev_space != nullptr && IsImmuneSpace(prev_space)) {
91 immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_);
92 immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_);
93 }
94 }
95}
96
97void SemiSpace::BindBitmaps() {
98 timings_.StartSplit("BindBitmaps");
99 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
100 // Mark all of the spaces we never collect as immune.
101 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800102 if (space->GetLiveBitmap() != nullptr) {
103 if (space == to_space_) {
104 BindLiveToMarkBitmap(to_space_);
105 } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect
106 || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect) {
107 ImmuneSpace(space);
108 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700109 }
110 }
111 timings_.EndSplit();
112}
113
114SemiSpace::SemiSpace(Heap* heap, const std::string& name_prefix)
115 : GarbageCollector(heap,
116 name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"),
117 mark_stack_(nullptr),
118 immune_begin_(nullptr),
119 immune_end_(nullptr),
120 to_space_(nullptr),
121 from_space_(nullptr),
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800122 self_(nullptr),
123 last_gc_to_space_end_(nullptr),
124 bytes_promoted_(0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700125}
126
127void SemiSpace::InitializePhase() {
128 timings_.Reset();
Ian Rogers5fe9af72013-11-14 00:17:20 -0800129 TimingLogger::ScopedSplit split("InitializePhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700130 mark_stack_ = heap_->mark_stack_.get();
131 DCHECK(mark_stack_ != nullptr);
132 immune_begin_ = nullptr;
133 immune_end_ = nullptr;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700134 self_ = Thread::Current();
135 // Do any pre GC verification.
136 timings_.NewSplit("PreGcVerification");
137 heap_->PreGcVerification(this);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800138 // Set the initial bitmap.
139 to_space_live_bitmap_ = to_space_->GetLiveBitmap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700140}
141
142void SemiSpace::ProcessReferences(Thread* self) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800143 TimingLogger::ScopedSplit split("ProcessReferences", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700144 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800145 GetHeap()->ProcessReferences(timings_, clear_soft_references_, &MarkedForwardingAddressCallback,
146 &RecursiveMarkObjectCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700147}
148
149void SemiSpace::MarkingPhase() {
150 Thread* self = Thread::Current();
151 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers5fe9af72013-11-14 00:17:20 -0800152 TimingLogger::ScopedSplit split("MarkingPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700153 // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the
154 // wrong space.
155 heap_->SwapSemiSpaces();
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800156 if (kEnableSimplePromo) {
157 // If last_gc_to_space_end_ is out of the bounds of the from-space
158 // (the to-space from last GC), then point it to the beginning of
159 // the from-space. For example, the very first GC or the
160 // pre-zygote compaction.
161 if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) {
162 last_gc_to_space_end_ = from_space_->Begin();
163 }
164 // Reset this before the marking starts below.
165 bytes_promoted_ = 0;
166 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700167 // Assume the cleared space is already empty.
168 BindBitmaps();
169 // Process dirty cards and add dirty cards to mod-union tables.
170 heap_->ProcessCards(timings_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800171 // Clear the whole card table since we can not get any additional dirty cards during the
172 // paused GC. This saves memory but only works for pause the world collectors.
173 timings_.NewSplit("ClearCardTable");
174 heap_->GetCardTable()->ClearCardTable();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700175 // Need to do this before the checkpoint since we don't want any threads to add references to
176 // the live stack during the recursive mark.
177 timings_.NewSplit("SwapStacks");
178 heap_->SwapStacks();
179 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
180 MarkRoots();
181 // Mark roots of immune spaces.
182 UpdateAndMarkModUnion();
183 // Recursively mark remaining objects.
184 MarkReachableObjects();
185}
186
187bool SemiSpace::IsImmuneSpace(const space::ContinuousSpace* space) const {
188 return
189 immune_begin_ <= reinterpret_cast<Object*>(space->Begin()) &&
190 immune_end_ >= reinterpret_cast<Object*>(space->End());
191}
192
193void SemiSpace::UpdateAndMarkModUnion() {
194 for (auto& space : heap_->GetContinuousSpaces()) {
195 // If the space is immune then we need to mark the references to other spaces.
196 if (IsImmuneSpace(space)) {
197 accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space);
198 CHECK(table != nullptr);
199 // TODO: Improve naming.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800200 TimingLogger::ScopedSplit split(
Mathieu Chartier590fee92013-09-13 13:46:47 -0700201 space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" :
202 "UpdateAndMarkImageModUnionTable",
203 &timings_);
204 table->UpdateAndMarkReferences(MarkRootCallback, this);
205 }
206 }
207}
208
209void SemiSpace::MarkReachableObjects() {
210 timings_.StartSplit("MarkStackAsLive");
211 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
212 heap_->MarkAllocStackAsLive(live_stack);
213 live_stack->Reset();
214 timings_.EndSplit();
215 // Recursively process the mark stack.
216 ProcessMarkStack(true);
217}
218
219void SemiSpace::ReclaimPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800220 TimingLogger::ScopedSplit split("ReclaimPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700221 Thread* self = Thread::Current();
222 ProcessReferences(self);
223 {
224 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
225 SweepSystemWeaks();
226 }
227 // Record freed memory.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800228 uint64_t from_bytes = from_space_->GetBytesAllocated();
229 uint64_t to_bytes = to_space_->GetBytesAllocated();
230 uint64_t from_objects = from_space_->GetObjectsAllocated();
231 uint64_t to_objects = to_space_->GetObjectsAllocated();
232 CHECK_LE(to_objects, from_objects);
233 int64_t freed_bytes = from_bytes - to_bytes;
234 int64_t freed_objects = from_objects - to_objects;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800235 freed_bytes_.FetchAndAdd(freed_bytes);
236 freed_objects_.FetchAndAdd(freed_objects);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800237 // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed
238 // space.
239 heap_->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700240 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 }
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800271
272 if (kEnableSimplePromo) {
273 // Record the end (top) of the to space so we can distinguish
274 // between objects that were allocated since the last GC and the
275 // older objects.
276 last_gc_to_space_end_ = to_space_->End();
277 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700278}
279
280void SemiSpace::ResizeMarkStack(size_t new_size) {
281 std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End());
282 CHECK_LE(mark_stack_->Size(), new_size);
283 mark_stack_->Resize(new_size);
284 for (const auto& obj : temp) {
285 mark_stack_->PushBack(obj);
286 }
287}
288
289inline void SemiSpace::MarkStackPush(Object* obj) {
290 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
291 ResizeMarkStack(mark_stack_->Capacity() * 2);
292 }
293 // The object must be pushed on to the mark stack.
294 mark_stack_->PushBack(obj);
295}
296
297// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
298bool SemiSpace::MarkLargeObject(const Object* obj) {
299 // TODO: support >1 discontinuous space.
300 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
301 accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
302 if (UNLIKELY(!large_objects->Test(obj))) {
303 large_objects->Set(obj);
304 return true;
305 }
306 return false;
307}
308
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800309mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) {
310 size_t object_size = obj->SizeOf();
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800311 size_t bytes_allocated;
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800312 mirror::Object* forward_address = nullptr;
313 if (kEnableSimplePromo && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) {
314 // If it's allocated before the last GC (older), move (pseudo-promote) it to
315 // the non-moving space (as sort of an old generation).
316 size_t bytes_promoted;
317 space::MallocSpace* non_moving_space = GetHeap()->GetNonMovingSpace();
318 forward_address = non_moving_space->Alloc(self_, object_size, &bytes_promoted);
319 if (forward_address == nullptr) {
320 // If out of space, fall back to the to-space.
321 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated);
322 } else {
323 GetHeap()->num_bytes_allocated_.FetchAndAdd(bytes_promoted);
324 bytes_promoted_ += bytes_promoted;
325 // Mark forward_address on the live bit map.
326 accounting::SpaceBitmap* live_bitmap = non_moving_space->GetLiveBitmap();
327 DCHECK(live_bitmap != nullptr);
328 DCHECK(!live_bitmap->Test(forward_address));
329 live_bitmap->Set(forward_address);
330 // Mark forward_address on the mark bit map.
331 accounting::SpaceBitmap* mark_bitmap = non_moving_space->GetMarkBitmap();
332 DCHECK(mark_bitmap != nullptr);
333 DCHECK(!mark_bitmap->Test(forward_address));
334 mark_bitmap->Set(forward_address);
335 }
336 DCHECK(forward_address != nullptr);
337 } else {
338 // If it's allocated after the last GC (younger), copy it to the to-space.
339 forward_address = to_space_->Alloc(self_, object_size, &bytes_allocated);
340 }
341 // Copy over the object and add it to the mark stack since we still need to update its
342 // references.
343 memcpy(reinterpret_cast<void*>(forward_address), obj, object_size);
344 if (to_space_live_bitmap_ != nullptr) {
345 to_space_live_bitmap_->Set(forward_address);
346 }
Mathieu Chartier5dc08a62014-01-10 10:10:23 -0800347 DCHECK(to_space_->HasAddress(forward_address) ||
348 (kEnableSimplePromo && GetHeap()->GetNonMovingSpace()->HasAddress(forward_address)));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800349 return forward_address;
350}
351
Mathieu Chartier590fee92013-09-13 13:46:47 -0700352// Used to mark and copy objects. Any newly-marked objects who are in the from space get moved to
353// the to-space and have their forward address updated. Objects which have been newly marked are
354// pushed on the mark stack.
355Object* SemiSpace::MarkObject(Object* obj) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800356 Object* forward_address = obj;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700357 if (obj != nullptr && !IsImmune(obj)) {
358 if (from_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800359 forward_address = GetForwardingAddressInFromSpace(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700360 // If the object has already been moved, return the new forward address.
Hiroshi Yamauchi4b1782f2013-12-05 16:46:22 -0800361 if (forward_address == nullptr) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800362 forward_address = MarkNonForwardedObject(obj);
363 DCHECK(forward_address != nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700364 // Make sure to only update the forwarding address AFTER you copy the object so that the
365 // monitor word doesn't get stomped over.
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800366 obj->SetLockWord(LockWord::FromForwardingAddress(
367 reinterpret_cast<size_t>(forward_address)));
368 // Push the object onto the mark stack for later processing.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700369 MarkStackPush(forward_address);
370 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700371 // TODO: Do we need this if in the else statement?
372 } else {
373 accounting::SpaceBitmap* object_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
374 if (LIKELY(object_bitmap != nullptr)) {
375 // This object was not previously marked.
376 if (!object_bitmap->Test(obj)) {
377 object_bitmap->Set(obj);
378 MarkStackPush(obj);
379 }
380 } else {
381 DCHECK(!to_space_->HasAddress(obj)) << "Marking object in to_space_";
382 if (MarkLargeObject(obj)) {
383 MarkStackPush(obj);
384 }
385 }
386 }
387 }
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800388 return forward_address;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700389}
390
Mathieu Chartier39e32612013-11-12 16:28:05 -0800391Object* SemiSpace::RecursiveMarkObjectCallback(Object* root, void* arg) {
392 DCHECK(root != nullptr);
393 DCHECK(arg != nullptr);
394 SemiSpace* semi_space = reinterpret_cast<SemiSpace*>(arg);
395 mirror::Object* ret = semi_space->MarkObject(root);
396 semi_space->ProcessMarkStack(true);
397 return ret;
398}
399
Mathieu Chartier590fee92013-09-13 13:46:47 -0700400Object* SemiSpace::MarkRootCallback(Object* root, void* arg) {
401 DCHECK(root != nullptr);
402 DCHECK(arg != nullptr);
403 return reinterpret_cast<SemiSpace*>(arg)->MarkObject(root);
404}
405
406// Marks all objects in the root set.
407void SemiSpace::MarkRoots() {
408 timings_.StartSplit("MarkRoots");
409 // TODO: Visit up image roots as well?
410 Runtime::Current()->VisitRoots(MarkRootCallback, this, false, true);
411 timings_.EndSplit();
412}
413
414void SemiSpace::BindLiveToMarkBitmap(space::ContinuousSpace* space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700415 CHECK(space->IsMallocSpace());
416 space::MallocSpace* alloc_space = space->AsMallocSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700417 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
418 accounting::SpaceBitmap* mark_bitmap = alloc_space->BindLiveToMarkBitmap();
419 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
420}
421
Mathieu Chartier39e32612013-11-12 16:28:05 -0800422mirror::Object* SemiSpace::MarkedForwardingAddressCallback(Object* object, void* arg) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700423 return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object);
424}
425
426void SemiSpace::SweepSystemWeaks() {
427 timings_.StartSplit("SweepSystemWeaks");
Mathieu Chartier39e32612013-11-12 16:28:05 -0800428 Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700429 timings_.EndSplit();
430}
431
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800432bool SemiSpace::ShouldSweepSpace(space::MallocSpace* space) const {
433 return space != from_space_ && space != to_space_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700434}
435
436void SemiSpace::Sweep(bool swap_bitmaps) {
437 DCHECK(mark_stack_->IsEmpty());
Ian Rogers5fe9af72013-11-14 00:17:20 -0800438 TimingLogger::ScopedSplit("Sweep", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700439 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800440 if (space->IsMallocSpace()) {
Mathieu Chartierec050072014-01-07 16:00:07 -0800441 space::MallocSpace* malloc_space = space->AsMallocSpace();
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800442 if (!ShouldSweepSpace(malloc_space)) {
443 continue;
444 }
Mathieu Chartierec050072014-01-07 16:00:07 -0800445 TimingLogger::ScopedSplit split(
446 malloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", &timings_);
447 size_t freed_objects = 0;
448 size_t freed_bytes = 0;
449 malloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes);
450 heap_->RecordFree(freed_objects, freed_bytes);
451 freed_objects_.FetchAndAdd(freed_objects);
452 freed_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700453 }
454 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700455 SweepLargeObjects(swap_bitmaps);
456}
457
458void SemiSpace::SweepLargeObjects(bool swap_bitmaps) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800459 TimingLogger::ScopedSplit("SweepLargeObjects", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700460 // Sweep large objects
461 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
462 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
463 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
464 if (swap_bitmaps) {
465 std::swap(large_live_objects, large_mark_objects);
466 }
467 // O(n*log(n)) but hopefully there are not too many large objects.
468 size_t freed_objects = 0;
469 size_t freed_bytes = 0;
470 Thread* self = Thread::Current();
471 for (const Object* obj : large_live_objects->GetObjects()) {
472 if (!large_mark_objects->Test(obj)) {
473 freed_bytes += large_object_space->Free(self, const_cast<Object*>(obj));
474 ++freed_objects;
475 }
476 }
Ian Rogersb122a4b2013-11-19 18:00:50 -0800477 freed_large_objects_.FetchAndAdd(freed_objects);
478 freed_large_object_bytes_.FetchAndAdd(freed_bytes);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700479 GetHeap()->RecordFree(freed_objects, freed_bytes);
480}
481
482// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
483// marked, put it on the appropriate list in the heap for later processing.
484void SemiSpace::DelayReferenceReferent(mirror::Class* klass, Object* obj) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800485 heap_->DelayReferenceReferent(klass, obj, MarkedForwardingAddressCallback, this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700486}
487
488// Visit all of the references of an object and update.
489void SemiSpace::ScanObject(Object* obj) {
490 DCHECK(obj != NULL);
491 DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space";
492 MarkSweep::VisitObjectReferences(obj, [this](Object* obj, Object* ref, const MemberOffset& offset,
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100493 bool /* is_static */) ALWAYS_INLINE_LAMBDA NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700494 mirror::Object* new_address = MarkObject(ref);
495 if (new_address != ref) {
496 DCHECK(new_address != nullptr);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800497 // Don't need to mark the card since we updating the object address and not changing the
498 // actual objects its pointing to. Using SetFieldPtr is better in this case since it does not
499 // dirty cards and use additional memory.
500 obj->SetFieldPtr(offset, new_address, false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700501 }
502 }, kMovingClasses);
503 mirror::Class* klass = obj->GetClass();
504 if (UNLIKELY(klass->IsReferenceClass())) {
505 DelayReferenceReferent(klass, obj);
506 }
507}
508
509// Scan anything that's on the mark stack.
510void SemiSpace::ProcessMarkStack(bool paused) {
511 timings_.StartSplit(paused ? "(paused)ProcessMarkStack" : "ProcessMarkStack");
512 while (!mark_stack_->IsEmpty()) {
513 ScanObject(mark_stack_->PopBack());
514 }
515 timings_.EndSplit();
516}
517
Mathieu Chartier590fee92013-09-13 13:46:47 -0700518inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const
519 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
520 // All immune objects are assumed marked.
521 if (IsImmune(obj)) {
522 return obj;
523 }
524 if (from_space_->HasAddress(obj)) {
525 mirror::Object* forwarding_address = GetForwardingAddressInFromSpace(const_cast<Object*>(obj));
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800526 return forwarding_address; // Returns either the forwarding address or nullptr.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700527 } else if (to_space_->HasAddress(obj)) {
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800528 // Should be unlikely.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700529 // Already forwarded, must be marked.
530 return obj;
531 }
532 return heap_->GetMarkBitmap()->Test(obj) ? obj : nullptr;
533}
534
Mathieu Chartier590fee92013-09-13 13:46:47 -0700535void SemiSpace::UnBindBitmaps() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800536 TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700537 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700538 if (space->IsMallocSpace()) {
539 space::MallocSpace* alloc_space = space->AsMallocSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700540 if (alloc_space->HasBoundBitmaps()) {
541 alloc_space->UnBindBitmaps();
542 heap_->GetMarkBitmap()->ReplaceBitmap(alloc_space->GetLiveBitmap(),
543 alloc_space->GetMarkBitmap());
544 }
545 }
546 }
547}
548
549void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) {
550 DCHECK(to_space != nullptr);
551 to_space_ = to_space;
552}
553
554void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) {
555 DCHECK(from_space != nullptr);
556 from_space_ = from_space;
557}
558
559void SemiSpace::FinishPhase() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800560 TimingLogger::ScopedSplit split("FinishPhase", &timings_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700561 // Can't enqueue references if we hold the mutator lock.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700562 Heap* heap = GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700563 timings_.NewSplit("PostGcVerification");
564 heap->PostGcVerification(this);
565
566 // Null the "to" and "from" spaces since compacting from one to the other isn't valid until
567 // further action is done by the heap.
568 to_space_ = nullptr;
569 from_space_ = nullptr;
570
571 // Update the cumulative statistics
Mathieu Chartier590fee92013-09-13 13:46:47 -0700572 total_freed_objects_ += GetFreedObjects() + GetFreedLargeObjects();
573 total_freed_bytes_ += GetFreedBytes() + GetFreedLargeObjectBytes();
574
575 // Ensure that the mark stack is empty.
576 CHECK(mark_stack_->IsEmpty());
577
578 // Update the cumulative loggers.
579 cumulative_timings_.Start();
580 cumulative_timings_.AddLogger(timings_);
581 cumulative_timings_.End();
582
583 // Clear all of the spaces' mark bitmaps.
584 for (const auto& space : GetHeap()->GetContinuousSpaces()) {
585 accounting::SpaceBitmap* bitmap = space->GetMarkBitmap();
586 if (bitmap != nullptr &&
587 space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
588 bitmap->Clear();
589 }
590 }
591 mark_stack_->Reset();
592
593 // Reset the marked large objects.
594 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
595 large_objects->GetMarkObjects()->Clear();
596}
597
598} // namespace collector
599} // namespace gc
600} // namespace art