blob: 279796f38df911376e7e5f5da6015d47965e0f38 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "mark_sweep.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Mathieu Chartier2b82db42012-11-14 17:29:05 -080019#include <functional>
20#include <numeric>
Carl Shapiro58551df2011-07-24 03:09:51 -070021#include <climits>
22#include <vector>
23
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/macros.h"
Ian Rogers693ff612013-02-01 10:56:12 -080026#include "base/mutex-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080027#include "base/timing_logger.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
29#include "gc/accounting/heap_bitmap.h"
30#include "gc/accounting/space_bitmap-inl.h"
31#include "gc/heap.h"
32#include "gc/space/image_space.h"
33#include "gc/space/large_object_space.h"
34#include "gc/space/space-inl.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070035#include "indirect_reference_table.h"
36#include "intern_table.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070037#include "jni_internal.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070038#include "monitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mark_sweep-inl.h"
40#include "mirror/class-inl.h"
41#include "mirror/class_loader.h"
42#include "mirror/dex_cache.h"
43#include "mirror/field.h"
44#include "mirror/field-inl.h"
45#include "mirror/object-inl.h"
46#include "mirror/object_array.h"
47#include "mirror/object_array-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070048#include "runtime.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070049#include "thread-inl.h"
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070050#include "thread_list.h"
Ian Rogers08254272012-10-23 17:49:23 -070051#include "verifier/method_verifier.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070052
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053using namespace art::mirror;
54
Carl Shapiro69759ea2011-07-21 18:13:35 -070055namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070056namespace gc {
57namespace collector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070058
Mathieu Chartier02b6a782012-10-26 13:51:26 -070059// Performance options.
60static const bool kParallelMarkStack = true;
Mathieu Chartierbdd0fb92013-07-02 10:16:15 -070061static const bool kDisableFinger = true; // TODO: Fix, bit rotten.
Mathieu Chartier858f1c52012-10-17 17:45:55 -070062static const bool kUseMarkStackPrefetch = true;
63
Mathieu Chartier02b6a782012-10-26 13:51:26 -070064// Profiling and information flags.
65static const bool kCountClassesMarked = false;
66static const bool kProfileLargeObjects = false;
67static const bool kMeasureOverhead = false;
68static const bool kCountTasks = false;
Mathieu Chartierd22d5482012-11-06 17:14:12 -080069static const bool kCountJavaLangRefs = false;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070070
Mathieu Chartier357e9be2012-08-01 11:00:14 -070071class SetFingerVisitor {
72 public:
73 SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070074 }
75
76 void operator ()(void* finger) const {
77 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
78 }
79
80 private:
81 MarkSweep* const mark_sweep_;
82};
83
Ian Rogers1d54e732013-05-02 21:10:01 -070084void MarkSweep::ImmuneSpace(space::ContinuousSpace* space) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -080085 // Bind live to mark bitmap if necessary.
86 if (space->GetLiveBitmap() != space->GetMarkBitmap()) {
87 BindLiveToMarkBitmap(space);
88 }
89
90 // Add the space to the immune region.
91 if (immune_begin_ == NULL) {
92 DCHECK(immune_end_ == NULL);
93 SetImmuneRange(reinterpret_cast<Object*>(space->Begin()),
94 reinterpret_cast<Object*>(space->End()));
95 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -070096 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
97 const space::ContinuousSpace* prev_space = NULL;
98 // Find out if the previous space is immune.
99 // TODO: C++0x
100 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
101 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
102 if (*it == space) {
103 break;
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800104 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700105 prev_space = *it;
106 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800107
Ian Rogers1d54e732013-05-02 21:10:01 -0700108 // If previous space was immune, then extend the immune region. Relies on continuous spaces
109 // being sorted by Heap::AddContinuousSpace.
110 if (prev_space != NULL &&
111 immune_begin_ <= reinterpret_cast<Object*>(prev_space->Begin()) &&
112 immune_end_ >= reinterpret_cast<Object*>(prev_space->End())) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800113 immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_);
114 immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_);
115 }
116 }
117}
118
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800119void MarkSweep::BindBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700120 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800121 WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
122
123 // Mark all of the spaces we never collect as immune.
Ian Rogers1d54e732013-05-02 21:10:01 -0700124 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
125 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
126 space::ContinuousSpace* space = *it;
127 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800128 ImmuneSpace(space);
129 }
130 }
131}
132
Ian Rogers1d54e732013-05-02 21:10:01 -0700133MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix)
134 : GarbageCollector(heap,
135 name_prefix + (name_prefix.empty() ? "" : " ") +
136 (is_concurrent ? "concurrent mark sweep": "mark sweep")),
137 current_mark_bitmap_(NULL),
138 java_lang_Class_(NULL),
139 mark_stack_(NULL),
140 finger_(NULL),
141 immune_begin_(NULL),
142 immune_end_(NULL),
143 soft_reference_list_(NULL),
144 weak_reference_list_(NULL),
145 finalizer_reference_list_(NULL),
146 phantom_reference_list_(NULL),
147 cleared_reference_list_(NULL),
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800148 gc_barrier_(new Barrier(0)),
Ian Rogers62d6c772013-02-27 08:32:07 -0800149 large_object_lock_("mark sweep large object lock", kMarkSweepLargeObjectLock),
150 mark_stack_expand_lock_("mark sweep mark stack expand lock"),
Ian Rogers1bd4b4c2013-04-18 17:47:42 -0700151 is_concurrent_(is_concurrent),
Ian Rogers1d54e732013-05-02 21:10:01 -0700152 clear_soft_references_(false) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800153}
154
155void MarkSweep::InitializePhase() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700156 timings_.Reset();
157 timings_.StartSplit("InitializePhase");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800158 mark_stack_ = GetHeap()->mark_stack_.get();
159 DCHECK(mark_stack_ != NULL);
160 finger_ = NULL;
161 SetImmuneRange(NULL, NULL);
162 soft_reference_list_ = NULL;
163 weak_reference_list_ = NULL;
164 finalizer_reference_list_ = NULL;
165 phantom_reference_list_ = NULL;
166 cleared_reference_list_ = NULL;
167 freed_bytes_ = 0;
168 freed_objects_ = 0;
169 class_count_ = 0;
170 array_count_ = 0;
171 other_count_ = 0;
172 large_object_test_ = 0;
173 large_object_mark_ = 0;
174 classes_marked_ = 0;
175 overhead_time_ = 0;
176 work_chunks_created_ = 0;
177 work_chunks_deleted_ = 0;
178 reference_count_ = 0;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700179 java_lang_Class_ = Class::GetJavaLangClass();
180 CHECK(java_lang_Class_ != NULL);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700181 FindDefaultMarkBitmap();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800182 // Do any pre GC verification.
183 heap_->PreGcVerification(this);
184}
185
186void MarkSweep::ProcessReferences(Thread* self) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700187 timings_.NewSplit("ProcessReferences");
Mathieu Chartier8e56c7e2012-11-20 13:25:50 -0800188 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800189 ProcessReferences(&soft_reference_list_, clear_soft_references_, &weak_reference_list_,
190 &finalizer_reference_list_, &phantom_reference_list_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800191}
192
193bool MarkSweep::HandleDirtyObjectsPhase() {
194 Thread* self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700195 accounting::ObjectStack* allocation_stack = GetHeap()->allocation_stack_.get();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800196 Locks::mutator_lock_->AssertExclusiveHeld(self);
197
198 {
Ian Rogers1d54e732013-05-02 21:10:01 -0700199 timings_.NewSplit("ReMarkRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800200 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
201
202 // Re-mark root set.
203 ReMarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800204
205 // Scan dirty objects, this is only required if we are not doing concurrent GC.
Ian Rogers1d54e732013-05-02 21:10:01 -0700206 RecursiveMarkDirtyObjects(accounting::CardTable::kCardDirty);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800207 }
208
209 ProcessReferences(self);
210
211 // Only need to do this if we have the card mark verification on, and only during concurrent GC.
212 if (GetHeap()->verify_missing_card_marks_) {
213 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
214 // This second sweep makes sure that we don't have any objects in the live stack which point to
215 // freed objects. These cause problems since their references may be previously freed objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700216 SweepArray(allocation_stack, false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800217 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700218 timings_.NewSplit("UnMarkAllocStack");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800219 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700220 // The allocation stack contains things allocated since the start of the GC. These may have been
221 // marked during this GC meaning they won't be eligible for reclaiming in the next sticky GC.
222 // Remove these objects from the mark bitmaps so that they will be eligible for sticky
223 // collection.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800224 heap_->UnMarkAllocStack(GetHeap()->alloc_space_->GetMarkBitmap(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700225 GetHeap()->large_object_space_->GetMarkObjects(),
226 allocation_stack);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800227 }
228 return true;
229}
230
231bool MarkSweep::IsConcurrent() const {
232 return is_concurrent_;
233}
234
235void MarkSweep::MarkingPhase() {
236 Heap* heap = GetHeap();
237 Thread* self = Thread::Current();
238
Ian Rogers1d54e732013-05-02 21:10:01 -0700239 timings_.NewSplit("BindBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800240 BindBitmaps();
241 FindDefaultMarkBitmap();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800242 // Process dirty cards and add dirty cards to mod union tables.
243 heap->ProcessCards(timings_);
244
245 // Need to do this before the checkpoint since we don't want any threads to add references to
246 // the live stack during the recursive mark.
Ian Rogers1d54e732013-05-02 21:10:01 -0700247 timings_.NewSplit("SwapStacks");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800248 heap->SwapStacks();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800249
250 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
251 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
252 // If we exclusively hold the mutator lock, all threads must be suspended.
Ian Rogers1d54e732013-05-02 21:10:01 -0700253 timings_.NewSplit("MarkRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800254 MarkRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800255 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700256 timings_.NewSplit("MarkRootsCheckpoint");
257 MarkRootsCheckpoint(self);
258 timings_.NewSplit("MarkNonThreadRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800259 MarkNonThreadRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800260 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700261 timings_.NewSplit("MarkConcurrentRoots");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800262 MarkConcurrentRoots();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800263
264 heap->UpdateAndMarkModUnion(this, timings_, GetGcType());
265 MarkReachableObjects();
266}
267
268void MarkSweep::MarkReachableObjects() {
269 // Mark everything allocated since the last as GC live so that we can sweep concurrently,
270 // knowing that new allocations won't be marked as live.
Ian Rogers1d54e732013-05-02 21:10:01 -0700271 timings_.NewSplit("MarkStackAsLive");
272 accounting::ObjectStack* live_stack = heap_->GetLiveStack();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800273 heap_->MarkAllocStack(heap_->alloc_space_->GetLiveBitmap(),
274 heap_->large_object_space_->GetLiveObjects(),
275 live_stack);
276 live_stack->Reset();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800277 // Recursively mark all the non-image bits set in the mark bitmap.
278 RecursiveMark();
279 DisableFinger();
280}
281
282void MarkSweep::ReclaimPhase() {
283 Thread* self = Thread::Current();
284
285 if (!IsConcurrent()) {
286 ProcessReferences(self);
287 }
288
289 // Before freeing anything, lets verify the heap.
290 if (kIsDebugBuild) {
291 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
292 VerifyImageRoots();
293 }
294 heap_->PreSweepingGcVerification(this);
295
296 {
297 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
298
299 // Reclaim unmarked objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700300 Sweep(false);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800301
302 // Swap the live and mark bitmaps for each space which we modified space. This is an
303 // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound
304 // bitmaps.
Ian Rogers1d54e732013-05-02 21:10:01 -0700305 timings_.NewSplit("SwapBitmaps");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800306 SwapBitmaps();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800307
308 // Unbind the live and mark bitmaps.
309 UnBindBitmaps();
310 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800311}
312
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800313void MarkSweep::SetImmuneRange(Object* begin, Object* end) {
314 immune_begin_ = begin;
315 immune_end_ = end;
Carl Shapiro58551df2011-07-24 03:09:51 -0700316}
317
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700318void MarkSweep::FindDefaultMarkBitmap() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700319 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
320 // TODO: C++0x
321 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
322 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
323 space::ContinuousSpace* space = *it;
324 if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700325 current_mark_bitmap_ = (*it)->GetMarkBitmap();
326 CHECK(current_mark_bitmap_ != NULL);
327 return;
328 }
329 }
330 GetHeap()->DumpSpaces();
331 LOG(FATAL) << "Could not find a default mark bitmap";
332}
333
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800334void MarkSweep::ExpandMarkStack() {
335 // Rare case, no need to have Thread::Current be a parameter.
336 MutexLock mu(Thread::Current(), mark_stack_expand_lock_);
337 if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) {
338 // Someone else acquired the lock and expanded the mark stack before us.
339 return;
340 }
341 std::vector<Object*> temp;
342 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
343 mark_stack_->Resize(mark_stack_->Capacity() * 2);
344 for (size_t i = 0; i < temp.size(); ++i) {
345 mark_stack_->PushBack(temp[i]);
346 }
347}
348
349inline void MarkSweep::MarkObjectNonNullParallel(const Object* obj, bool check_finger) {
350 DCHECK(obj != NULL);
351 if (MarkObjectParallel(obj)) {
352 if (kDisableFinger || (check_finger && obj < finger_)) {
353 while (UNLIKELY(!mark_stack_->AtomicPushBack(const_cast<Object*>(obj)))) {
354 // Only reason a push can fail is that the mark stack is full.
355 ExpandMarkStack();
356 }
357 }
358 }
359}
360
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700361inline void MarkSweep::MarkObjectNonNull(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700362 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700363
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700364 if (obj >= immune_begin_ && obj < immune_end_) {
365 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700366 return;
367 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700368
369 // Try to take advantage of locality of references within a space, failing this find the space
370 // the hard way.
Ian Rogers1d54e732013-05-02 21:10:01 -0700371 accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700372 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700373 accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
374 if (LIKELY(new_bitmap != NULL)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700375 object_bitmap = new_bitmap;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700376 } else {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700377 MarkLargeObject(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700378 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700379 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700380 }
381
Carl Shapiro69759ea2011-07-21 18:13:35 -0700382 // This object was not previously marked.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700383 if (!object_bitmap->Test(obj)) {
384 object_bitmap->Set(obj);
385 if (kDisableFinger || (check_finger && obj < finger_)) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700386 // Do we need to expand the mark stack?
387 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800388 ExpandMarkStack();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700389 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700390 // The object must be pushed on to the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700391 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700392 }
393 }
394}
395
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700396// Rare case, probably not worth inlining since it will increase instruction cache miss rate.
397bool MarkSweep::MarkLargeObject(const Object* obj) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700398 // TODO: support >1 discontinuous space.
399 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
400 accounting::SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700401 if (kProfileLargeObjects) {
402 ++large_object_test_;
403 }
404 if (UNLIKELY(!large_objects->Test(obj))) {
Ian Rogers8afe6e02013-06-12 19:40:25 -0700405 // TODO: mark may be called holding the JNI global references lock, Contains will hold the
406 // large object space lock causing a lock level violation. Bug: 9414652;
407 if (!kDebugLocking && !large_object_space->Contains(obj)) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700408 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
409 LOG(ERROR) << "Attempting see if it's a bad root";
410 VerifyRoots();
411 LOG(FATAL) << "Can't mark bad root";
412 }
413 if (kProfileLargeObjects) {
414 ++large_object_mark_;
415 }
416 large_objects->Set(obj);
417 // Don't need to check finger since large objects never have any object references.
418 return true;
419 }
420 return false;
421}
422
423inline bool MarkSweep::MarkObjectParallel(const Object* obj) {
424 DCHECK(obj != NULL);
425
426 if (obj >= immune_begin_ && obj < immune_end_) {
427 DCHECK(IsMarked(obj));
428 return false;
429 }
430
431 // Try to take advantage of locality of references within a space, failing this find the space
432 // the hard way.
Ian Rogers1d54e732013-05-02 21:10:01 -0700433 accounting::SpaceBitmap* object_bitmap = current_mark_bitmap_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700434 if (UNLIKELY(!object_bitmap->HasAddress(obj))) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700435 accounting::SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetContinuousSpaceBitmap(obj);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700436 if (new_bitmap != NULL) {
437 object_bitmap = new_bitmap;
438 } else {
439 // TODO: Remove the Thread::Current here?
440 // TODO: Convert this to some kind of atomic marking?
441 MutexLock mu(Thread::Current(), large_object_lock_);
442 return MarkLargeObject(obj);
443 }
444 }
445
446 // Return true if the object was not previously marked.
447 return !object_bitmap->AtomicTestAndSet(obj);
448}
449
Carl Shapiro69759ea2011-07-21 18:13:35 -0700450// Used to mark objects when recursing. Recursion is done by moving
451// the finger across the bitmaps in address order and marking child
452// objects. Any newly-marked objects whose addresses are lower than
453// the finger won't be visited by the bitmap scan, so those objects
454// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700455void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700456 if (obj != NULL) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700457 MarkObjectNonNull(obj, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700458 }
459}
460
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800461void MarkSweep::MarkRoot(const Object* obj) {
462 if (obj != NULL) {
463 MarkObjectNonNull(obj, false);
464 }
465}
466
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800467void MarkSweep::MarkRootParallelCallback(const Object* root, void* arg) {
468 DCHECK(root != NULL);
469 DCHECK(arg != NULL);
470 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
471 mark_sweep->MarkObjectNonNullParallel(root, false);
472}
473
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700474void MarkSweep::MarkObjectCallback(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700475 DCHECK(root != NULL);
476 DCHECK(arg != NULL);
477 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700478 mark_sweep->MarkObjectNonNull(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700479}
480
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700481void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
482 DCHECK(root != NULL);
483 DCHECK(arg != NULL);
484 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700485 mark_sweep->MarkObjectNonNull(root, true);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700486}
487
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700488void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
Ian Rogers40e3bac2012-11-20 00:09:14 -0800489 const StackVisitor* visitor) {
490 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700491}
492
Ian Rogers40e3bac2012-11-20 00:09:14 -0800493void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700494 // See if the root is on any space bitmap.
Ian Rogers1d54e732013-05-02 21:10:01 -0700495 if (GetHeap()->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == NULL) {
496 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier4202b742012-10-17 17:51:25 -0700497 if (!large_object_space->Contains(root)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700498 LOG(ERROR) << "Found invalid root: " << root;
Ian Rogers40e3bac2012-11-20 00:09:14 -0800499 if (visitor != NULL) {
500 LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700501 }
502 }
503 }
504}
505
506void MarkSweep::VerifyRoots() {
507 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
508}
509
Carl Shapiro69759ea2011-07-21 18:13:35 -0700510// Marks all objects in the root set.
511void MarkSweep::MarkRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700512 Runtime::Current()->VisitNonConcurrentRoots(MarkObjectCallback, this);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700513}
514
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700515void MarkSweep::MarkNonThreadRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700516 Runtime::Current()->VisitNonThreadRoots(MarkObjectCallback, this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700517}
518
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700519void MarkSweep::MarkConcurrentRoots() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700520 // Visit all runtime roots and clear dirty flags.
521 Runtime::Current()->VisitConcurrentRoots(MarkObjectCallback, this, false, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700522}
523
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700524class CheckObjectVisitor {
525 public:
526 CheckObjectVisitor(MarkSweep* const mark_sweep)
527 : mark_sweep_(mark_sweep) {
528
529 }
530
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700531 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800532 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800533 if (kDebugLocking) {
534 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
535 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700536 mark_sweep_->CheckReference(obj, ref, offset, is_static);
537 }
538
539 private:
540 MarkSweep* const mark_sweep_;
541};
542
543void MarkSweep::CheckObject(const Object* obj) {
544 DCHECK(obj != NULL);
545 CheckObjectVisitor visitor(this);
546 VisitObjectReferences(obj, visitor);
547}
548
549void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
550 DCHECK(root != NULL);
551 DCHECK(arg != NULL);
552 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700553 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700554 mark_sweep->CheckObject(root);
555}
556
Ian Rogers1d54e732013-05-02 21:10:01 -0700557void MarkSweep::BindLiveToMarkBitmap(space::ContinuousSpace* space) {
558 CHECK(space->IsDlMallocSpace());
559 space::DlMallocSpace* alloc_space = space->AsDlMallocSpace();
560 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
561 accounting::SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700562 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
563 alloc_space->temp_bitmap_.reset(mark_bitmap);
564 alloc_space->mark_bitmap_.reset(live_bitmap);
565}
566
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700567class ScanObjectVisitor {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700568 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700569 ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700570 }
571
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800572 // TODO: Fixme when anotatalysis works with visitors.
573 void operator ()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
574 if (kDebugLocking) {
575 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
576 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
577 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700578 mark_sweep_->ScanObject(obj);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700579 }
580
581 private:
582 MarkSweep* const mark_sweep_;
583};
584
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800585void MarkSweep::ScanGrayObjects(byte minimum_age) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700586 accounting::CardTable* card_table = GetHeap()->GetCardTable();
587 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700588 ScanObjectVisitor visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700589 SetFingerVisitor finger_visitor(this);
Ian Rogers1d54e732013-05-02 21:10:01 -0700590 // TODO: C++0x
591 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
592 for (It it = spaces.begin(), space_end = spaces.end(); it != space_end; ++it) {
593 space::ContinuousSpace* space = *it;
594 switch (space->GetGcRetentionPolicy()) {
595 case space::kGcRetentionPolicyNeverCollect:
596 timings_.NewSplit("ScanGrayImageSpaceObjects");
597 break;
598 case space::kGcRetentionPolicyFullCollect:
599 timings_.NewSplit("ScanGrayZygoteSpaceObjects");
600 break;
601 case space::kGcRetentionPolicyAlwaysCollect:
602 timings_.NewSplit("ScanGrayAllocSpaceObjects");
603 break;
604 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700605 byte* begin = space->Begin();
606 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700607 // Image spaces are handled properly since live == marked for them.
Ian Rogers1d54e732013-05-02 21:10:01 -0700608 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
609 card_table->Scan(mark_bitmap, begin, end, visitor, finger_visitor, minimum_age);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700610 }
611}
612
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700613class CheckBitmapVisitor {
614 public:
615 CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700616 }
617
Ian Rogers1d54e732013-05-02 21:10:01 -0700618 void operator ()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800619 if (kDebugLocking) {
620 Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current());
621 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700622 DCHECK(obj != NULL);
623 mark_sweep_->CheckObject(obj);
624 }
625
626 private:
627 MarkSweep* mark_sweep_;
628};
629
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700630void MarkSweep::VerifyImageRoots() {
631 // Verify roots ensures that all the references inside the image space point
632 // objects which are either in the image space or marked objects in the alloc
633 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700634 CheckBitmapVisitor visitor(this);
Ian Rogers1d54e732013-05-02 21:10:01 -0700635 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
636 // TODO: C++0x
637 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
638 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700639 if ((*it)->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700640 space::ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700641 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
642 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers1d54e732013-05-02 21:10:01 -0700643 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700644 DCHECK(live_bitmap != NULL);
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800645 live_bitmap->VisitMarkedRange(begin, end, visitor, VoidFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700646 }
647 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700648}
649
Carl Shapiro58551df2011-07-24 03:09:51 -0700650// Populates the mark stack based on the set of marked objects and
651// recursively marks until the mark stack is emptied.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800652void MarkSweep::RecursiveMark() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700653 timings_.NewSplit("RecursiveMark");
Brian Carlstrom1f870082011-08-23 16:02:11 -0700654 // RecursiveMark will build the lists of known instances of the Reference classes.
655 // See DelayReferenceReferent for details.
656 CHECK(soft_reference_list_ == NULL);
657 CHECK(weak_reference_list_ == NULL);
658 CHECK(finalizer_reference_list_ == NULL);
659 CHECK(phantom_reference_list_ == NULL);
660 CHECK(cleared_reference_list_ == NULL);
661
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800662 const bool partial = GetGcType() == kGcTypePartial;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700663 SetFingerVisitor set_finger_visitor(this);
664 ScanObjectVisitor scan_visitor(this);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800665 if (!kDisableFinger) {
666 finger_ = NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -0700667 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
668 // TODO: C++0x
669 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
670 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
671 space::ContinuousSpace* space = *it;
672 if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) ||
673 (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800674 current_mark_bitmap_ = space->GetMarkBitmap();
675 if (current_mark_bitmap_ == NULL) {
676 GetHeap()->DumpSpaces();
677 LOG(FATAL) << "invalid bitmap";
678 }
679 // This function does not handle heap end increasing, so we must use the space end.
680 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
681 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
682 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700683 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700684 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700685 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800686 DisableFinger();
Ian Rogers1d54e732013-05-02 21:10:01 -0700687 timings_.NewSplit("ProcessMarkStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700688 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700689}
690
691bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
692 return
693 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700694 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
695}
696
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800697void MarkSweep::RecursiveMarkDirtyObjects(byte minimum_age) {
698 ScanGrayObjects(minimum_age);
Ian Rogers1d54e732013-05-02 21:10:01 -0700699 timings_.NewSplit("ProcessMarkStack");
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700700 ProcessMarkStack();
701}
702
Carl Shapiro58551df2011-07-24 03:09:51 -0700703void MarkSweep::ReMarkRoots() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700704 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this, true, true);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700705}
706
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800707void MarkSweep::SweepJniWeakGlobals(IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700708 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700709 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700710 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700711 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700712 for (It it = table->begin(), end = table->end(); it != end; ++it) {
713 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700714 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700715 *entry = kClearedJniWeakGlobal;
716 }
717 }
718}
719
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700720struct ArrayMarkedCheck {
Ian Rogers1d54e732013-05-02 21:10:01 -0700721 accounting::ObjectStack* live_stack;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700722 MarkSweep* mark_sweep;
723};
724
725// Either marked or not live.
726bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
727 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
728 if (array_check->mark_sweep->IsMarked(object)) {
729 return true;
730 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700731 accounting::ObjectStack* live_stack = array_check->live_stack;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700732 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
733}
734
Ian Rogers1d54e732013-05-02 21:10:01 -0700735void MarkSweep::SweepSystemWeaksArray(accounting::ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700736 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700737 // The callbacks check
738 // !is_marked where is_marked is the callback but we want
739 // !IsMarked && IsLive
740 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
741 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700742
743 ArrayMarkedCheck visitor;
744 visitor.live_stack = allocations;
745 visitor.mark_sweep = this;
746 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
747 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
748 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
749}
750
751void MarkSweep::SweepSystemWeaks() {
752 Runtime* runtime = Runtime::Current();
753 // The callbacks check
754 // !is_marked where is_marked is the callback but we want
755 // !IsMarked && IsLive
756 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
757 // Or for swapped (IsLive || !IsMarked).
758 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
759 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
760 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700761}
762
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700763bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
764 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
765 // We don't actually want to sweep the object, so lets return "marked"
766 return true;
767}
768
769void MarkSweep::VerifyIsLive(const Object* obj) {
770 Heap* heap = GetHeap();
771 if (!heap->GetLiveBitmap()->Test(obj)) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700772 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700773 if (!large_object_space->GetLiveObjects()->Test(obj)) {
774 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
775 heap->allocation_stack_->End()) {
776 // Object not found!
777 heap->DumpSpaces();
778 LOG(FATAL) << "Found dead object " << obj;
779 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700780 }
781 }
782}
783
784void MarkSweep::VerifySystemWeaks() {
785 Runtime* runtime = Runtime::Current();
786 // Verify system weaks, uses a special IsMarked callback which always returns true.
787 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
788 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
789
790 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700791 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700792 IndirectReferenceTable* table = &vm->weak_globals;
793 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
794 for (It it = table->begin(), end = table->end(); it != end; ++it) {
795 const Object** entry = *it;
796 VerifyIsLive(*entry);
797 }
798}
799
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800800struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700801 MarkSweep* mark_sweep;
Ian Rogers1d54e732013-05-02 21:10:01 -0700802 space::AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700803 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800804};
805
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700806class CheckpointMarkThreadRoots : public Closure {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700807 public:
808 CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
809
810 }
811
812 virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
813 // Note: self is not necessarily equal to thread since thread may be suspended.
814 Thread* self = Thread::Current();
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800815 CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc)
816 << thread->GetState() << " thread " << thread << " self " << self;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800817 thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700818 mark_sweep_->GetBarrier().Pass(self);
819 }
820
821 private:
822 MarkSweep* mark_sweep_;
823};
824
Ian Rogers1d54e732013-05-02 21:10:01 -0700825void MarkSweep::MarkRootsCheckpoint(Thread* self) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800826 CheckpointMarkThreadRoots check_point(this);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700827 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Ian Rogers1d54e732013-05-02 21:10:01 -0700828 // Request the check point is run on all threads returning a count of the threads that must
829 // run through the barrier including self.
830 size_t barrier_count = thread_list->RunCheckpoint(&check_point);
831 // Release locks then wait for all mutator threads to pass the barrier.
832 // TODO: optimize to not release locks when there are no threads to wait for.
833 Locks::heap_bitmap_lock_->ExclusiveUnlock(self);
834 Locks::mutator_lock_->SharedUnlock(self);
835 ThreadState old_state = self->SetState(kWaitingForCheckPointsToRun);
836 CHECK_EQ(old_state, kWaitingPerformingGc);
837 gc_barrier_->Increment(self, barrier_count);
838 self->SetState(kWaitingPerformingGc);
839 Locks::mutator_lock_->SharedLock(self);
840 Locks::heap_bitmap_lock_->ExclusiveLock(self);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700841}
842
Ian Rogers30fab402012-01-23 15:43:46 -0800843void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800844 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700845 MarkSweep* mark_sweep = context->mark_sweep;
846 Heap* heap = mark_sweep->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -0700847 space::AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700848 Thread* self = context->self;
849 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700850 // Use a bulk free, that merges consecutive objects before freeing or free per object?
851 // Documentation suggests better free performance with merging, but this may be at the expensive
852 // of allocation.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700853 size_t freed_objects = num_ptrs;
854 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
855 size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700856 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700857 mark_sweep->freed_objects_ += freed_objects;
858 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700859}
860
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700861void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700862 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700863 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700864 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700865 // We don't free any actual memory to avoid dirtying the shared zygote pages.
866 for (size_t i = 0; i < num_ptrs; ++i) {
867 Object* obj = static_cast<Object*>(ptrs[i]);
868 heap->GetLiveBitmap()->Clear(obj);
869 heap->GetCardTable()->MarkCard(obj);
870 }
871}
872
Ian Rogers1d54e732013-05-02 21:10:01 -0700873void MarkSweep::SweepArray(accounting::ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700874 size_t freed_bytes = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700875 space::DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700876
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700877 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
878 // bitmap, resulting in occasional frees of Weaks which are still in use.
Ian Rogers1d54e732013-05-02 21:10:01 -0700879 timings_.NewSplit("SweepSystemWeaks");
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700880 SweepSystemWeaksArray(allocations);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700881
Ian Rogers1d54e732013-05-02 21:10:01 -0700882 timings_.NewSplit("Process allocation stack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700883 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
884 // going to free.
Ian Rogers1d54e732013-05-02 21:10:01 -0700885 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
886 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
887 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
888 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
889 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700890 if (swap_bitmaps) {
891 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700892 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700893 }
894
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700895 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700896 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700897 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700898 Object** out = objects;
899
900 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700901 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700902 for (size_t i = 0;i < count;++i) {
903 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700904 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
905 if (LIKELY(mark_bitmap->HasAddress(obj))) {
906 if (!mark_bitmap->Test(obj)) {
907 // Don't bother un-marking since we clear the mark bitmap anyways.
908 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700909 }
910 } else if (!large_mark_objects->Test(obj)) {
911 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700912 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700913 }
914 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700915 CHECK_EQ(count, allocations->Size());
916 timings_.NewSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700917
918 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700919 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700920 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700921 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700922 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700923 freed_objects_ += freed_objects;
924 freed_bytes_ += freed_bytes;
Ian Rogers1d54e732013-05-02 21:10:01 -0700925
926 timings_.NewSplit("ResetStack");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700927 allocations->Reset();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700928}
929
Ian Rogers1d54e732013-05-02 21:10:01 -0700930void MarkSweep::Sweep(bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700931 DCHECK(mark_stack_->IsEmpty());
932
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700933 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
934 // bitmap, resulting in occasional frees of Weaks which are still in use.
Ian Rogers1d54e732013-05-02 21:10:01 -0700935 timings_.NewSplit("SweepSystemWeaks");
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700936 SweepSystemWeaks();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700937
Ian Rogers1d54e732013-05-02 21:10:01 -0700938 const bool partial = (GetGcType() == kGcTypePartial);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800939 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700940 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700941 scc.self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700942 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
943 // TODO: C++0x
944 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
945 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
946 space::ContinuousSpace* space = *it;
947 // We always sweep always collect spaces.
948 bool sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect);
949 if (!partial && !sweep_space) {
950 // We sweep full collect spaces when the GC isn't a partial GC (ie its full).
951 sweep_space = (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect);
952 }
953 if (sweep_space) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700954 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
955 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Ian Rogers1d54e732013-05-02 21:10:01 -0700956 scc.space = space->AsDlMallocSpace();
957 accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap();
958 accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700959 if (swap_bitmaps) {
960 std::swap(live_bitmap, mark_bitmap);
961 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700962 if (!space->IsZygoteSpace()) {
963 timings_.NewSplit("SweepAllocSpace");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700964 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Ian Rogers1d54e732013-05-02 21:10:01 -0700965 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
966 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700967 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700968 timings_.NewSplit("SweepZygote");
969 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual
970 // memory.
971 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
972 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700973 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700974 }
975 }
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800976
Ian Rogers1d54e732013-05-02 21:10:01 -0700977 timings_.NewSplit("SweepLargeObjects");
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800978 SweepLargeObjects(swap_bitmaps);
Carl Shapiro58551df2011-07-24 03:09:51 -0700979}
980
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700981void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
982 // Sweep large objects
Ian Rogers1d54e732013-05-02 21:10:01 -0700983 space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
984 accounting::SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
985 accounting::SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700986 if (swap_bitmaps) {
987 std::swap(large_live_objects, large_mark_objects);
988 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700989 accounting::SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700990 // O(n*log(n)) but hopefully there are not too many large objects.
991 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700992 size_t freed_bytes = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700993 Thread* self = Thread::Current();
Ian Rogers1d54e732013-05-02 21:10:01 -0700994 // TODO: C++0x
995 typedef accounting::SpaceSetMap::Objects::iterator It;
996 for (It it = live_objects.begin(), end = live_objects.end(); it != end; ++it) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700997 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700998 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700999 ++freed_objects;
1000 }
1001 }
1002 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001003 freed_bytes_ += freed_bytes;
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001004 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001005}
1006
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001007void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001008 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1009 // TODO: C++0x
1010 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1011 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1012 space::ContinuousSpace* space = *it;
1013 if (space->IsDlMallocSpace() && space->Contains(ref)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001014 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001015
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001016 bool is_marked = IsMarked(ref);
1017 if (!is_marked) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001018 LOG(INFO) << *space;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001019 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
1020 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
1021 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
1022 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001023
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001024 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
1025 DCHECK(klass != NULL);
1026 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
1027 DCHECK(fields != NULL);
1028 bool found = false;
1029 for (int32_t i = 0; i < fields->GetLength(); ++i) {
1030 const Field* cur = fields->Get(i);
1031 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
1032 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
1033 found = true;
1034 break;
1035 }
1036 }
1037 if (!found) {
1038 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
1039 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001040
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001041 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
1042 if (!obj_marked) {
1043 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
1044 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
1045 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001046 }
1047 }
Ian Rogers5d76c432011-10-31 21:42:49 -07001048 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001049 break;
Ian Rogers5d76c432011-10-31 21:42:49 -07001050 }
1051}
1052
Carl Shapiro69759ea2011-07-21 18:13:35 -07001053// Process the "referent" field in a java.lang.ref.Reference. If the
1054// referent has not yet been marked, put it on the appropriate list in
1055// the gcHeap for later processing.
1056void MarkSweep::DelayReferenceReferent(Object* obj) {
1057 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -07001058 Class* klass = obj->GetClass();
1059 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001060 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001061 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
1062 Object* referent = heap_->GetReferenceReferent(obj);
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001063 if (kCountJavaLangRefs) {
1064 ++reference_count_;
1065 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001066 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001067 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001068 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001069 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001070 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001071 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001072 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001073 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001074 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001075 list = &phantom_reference_list_;
1076 }
Brian Carlstrom0796af02011-10-12 14:31:45 -07001077 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001078 // TODO: One lock per list?
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001079 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001080 }
1081}
1082
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001083void MarkSweep::ScanRoot(const Object* obj) {
1084 ScanObject(obj);
1085}
1086
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001087class MarkObjectVisitor {
1088 public:
1089 MarkObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
1090 }
1091
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001092 // TODO: Fixme when anotatalysis works with visitors.
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001093 void operator ()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */,
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001094 bool /* is_static */) const
1095 NO_THREAD_SAFETY_ANALYSIS {
1096 if (kDebugLocking) {
1097 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
1098 Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current());
1099 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001100 mark_sweep_->MarkObject(ref);
1101 }
1102
1103 private:
1104 MarkSweep* const mark_sweep_;
1105};
1106
Carl Shapiro69759ea2011-07-21 18:13:35 -07001107// Scans an object reference. Determines the type of the reference
1108// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001109void MarkSweep::ScanObject(const Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001110 MarkObjectVisitor visitor(this);
1111 ScanObjectVisit(obj, visitor);
1112}
1113
1114class MarkStackChunk : public Task {
Ian Rogers1d54e732013-05-02 21:10:01 -07001115 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001116 MarkStackChunk(ThreadPool* thread_pool, MarkSweep* mark_sweep, Object** begin, Object** end)
1117 : mark_sweep_(mark_sweep),
1118 thread_pool_(thread_pool),
1119 index_(0),
1120 length_(0),
1121 output_(NULL) {
1122 length_ = end - begin;
1123 if (begin != end) {
1124 // Cost not significant since we only do this for the initial set of mark stack chunks.
1125 memcpy(data_, begin, length_ * sizeof(*begin));
1126 }
1127 if (kCountTasks) {
1128 ++mark_sweep_->work_chunks_created_;
1129 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001130 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001131
1132 ~MarkStackChunk() {
1133 DCHECK(output_ == NULL || output_->length_ == 0);
1134 DCHECK_GE(index_, length_);
1135 delete output_;
1136 if (kCountTasks) {
1137 ++mark_sweep_->work_chunks_deleted_;
1138 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001139 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001140
1141 MarkSweep* const mark_sweep_;
1142 ThreadPool* const thread_pool_;
1143 static const size_t max_size = 1 * KB;
1144 // Index of which object we are scanning. Only needs to be atomic if we are doing work stealing.
1145 size_t index_;
1146 // Input / output mark stack. We add newly marked references to data_ until length reaches
1147 // max_size. This is an optimization so that less tasks are created.
1148 // TODO: Investigate using a bounded buffer FIFO.
1149 Object* data_[max_size];
1150 // How many elements in data_ we need to scan.
1151 size_t length_;
1152 // Output block, newly marked references get added to the ouput block so that another thread can
1153 // scan them.
1154 MarkStackChunk* output_;
1155
1156 class MarkObjectParallelVisitor {
1157 public:
1158 MarkObjectParallelVisitor(MarkStackChunk* chunk_task) : chunk_task_(chunk_task) {
1159
1160 }
1161
1162 void operator ()(const Object* /* obj */, const Object* ref,
1163 const MemberOffset& /* offset */, bool /* is_static */) const {
1164 if (ref != NULL && chunk_task_->mark_sweep_->MarkObjectParallel(ref)) {
1165 chunk_task_->MarkStackPush(ref);
1166 }
1167 }
1168
1169 private:
1170 MarkStackChunk* const chunk_task_;
1171 };
1172
1173 // Push an object into the block.
1174 // Don't need to use atomic ++ since we only one thread is writing to an output block at any
1175 // given time.
1176 void Push(Object* obj) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001177 CHECK(obj != NULL);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001178 data_[length_++] = obj;
1179 }
1180
1181 void MarkStackPush(const Object* obj) {
1182 if (static_cast<size_t>(length_) < max_size) {
1183 Push(const_cast<Object*>(obj));
1184 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -07001185 // Internal (thread-local) buffer is full, push to a new buffer instead.
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001186 if (UNLIKELY(output_ == NULL)) {
1187 AllocateOutputChunk();
1188 } else if (UNLIKELY(static_cast<size_t>(output_->length_) == max_size)) {
1189 // Output block is full, queue it up for processing and obtain a new block.
1190 EnqueueOutput();
1191 AllocateOutputChunk();
1192 }
1193 output_->Push(const_cast<Object*>(obj));
1194 }
1195 }
1196
1197 void ScanObject(Object* obj) {
1198 mark_sweep_->ScanObjectVisit(obj, MarkObjectParallelVisitor(this));
1199 }
1200
1201 void EnqueueOutput() {
1202 if (output_ != NULL) {
1203 uint64_t start = 0;
1204 if (kMeasureOverhead) {
1205 start = NanoTime();
1206 }
1207 thread_pool_->AddTask(Thread::Current(), output_);
1208 output_ = NULL;
1209 if (kMeasureOverhead) {
1210 mark_sweep_->overhead_time_ += NanoTime() - start;
1211 }
1212 }
1213 }
1214
1215 void AllocateOutputChunk() {
1216 uint64_t start = 0;
1217 if (kMeasureOverhead) {
1218 start = NanoTime();
1219 }
1220 output_ = new MarkStackChunk(thread_pool_, mark_sweep_, NULL, NULL);
1221 if (kMeasureOverhead) {
1222 mark_sweep_->overhead_time_ += NanoTime() - start;
1223 }
1224 }
1225
1226 void Finalize() {
1227 EnqueueOutput();
1228 delete this;
1229 }
1230
1231 // Scans all of the objects
1232 virtual void Run(Thread* self) {
Brian Carlstromd74e41b2013-03-24 23:47:01 -07001233 size_t index;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001234 while ((index = index_++) < length_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001235 if (kUseMarkStackPrefetch) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001236 static const size_t prefetch_look_ahead = 1;
1237 __builtin_prefetch(data_[std::min(index + prefetch_look_ahead, length_ - 1)]);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001238 }
1239 Object* obj = data_[index];
1240 DCHECK(obj != NULL);
1241 ScanObject(obj);
1242 }
1243 }
1244};
1245
1246void MarkSweep::ProcessMarkStackParallel() {
1247 CHECK(kDisableFinger) << "parallel mark stack processing cannot work when finger is enabled";
1248 Thread* self = Thread::Current();
1249 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1250 // Split the current mark stack up into work tasks.
1251 const size_t num_threads = thread_pool->GetThreadCount();
1252 const size_t stack_size = mark_stack_->Size();
1253 const size_t chunk_size =
1254 std::min((stack_size + num_threads - 1) / num_threads,
1255 static_cast<size_t>(MarkStackChunk::max_size));
1256 size_t index = 0;
1257 for (size_t i = 0; i < num_threads || index < stack_size; ++i) {
1258 Object** begin = &mark_stack_->Begin()[std::min(stack_size, index)];
1259 Object** end = &mark_stack_->Begin()[std::min(stack_size, index + chunk_size)];
1260 index += chunk_size;
1261 thread_pool->AddTask(self, new MarkStackChunk(thread_pool, this, begin, end));
1262 }
1263 thread_pool->StartWorkers(self);
Ian Rogers1d54e732013-05-02 21:10:01 -07001264 thread_pool->Wait(self, true, true);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001265 mark_stack_->Reset();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001266 //LOG(INFO) << "Idle wait time " << PrettyDuration(thread_pool->GetWaitTime());
1267 CHECK_EQ(work_chunks_created_, work_chunks_deleted_) << " some of the work chunks were leaked";
Carl Shapiro69759ea2011-07-21 18:13:35 -07001268}
1269
Ian Rogers5d76c432011-10-31 21:42:49 -07001270// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001271void MarkSweep::ProcessMarkStack() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001272 ThreadPool* thread_pool = GetHeap()->GetThreadPool();
1273 if (kParallelMarkStack && thread_pool != NULL && thread_pool->GetThreadCount() > 0) {
1274 ProcessMarkStackParallel();
1275 return;
1276 }
1277
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001278 if (kUseMarkStackPrefetch) {
1279 const size_t fifo_size = 4;
1280 const size_t fifo_mask = fifo_size - 1;
1281 const Object* fifo[fifo_size];
1282 for (size_t i = 0;i < fifo_size;++i) {
1283 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001284 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001285 size_t fifo_pos = 0;
1286 size_t fifo_count = 0;
1287 for (;;) {
1288 const Object* obj = fifo[fifo_pos & fifo_mask];
1289 if (obj != NULL) {
1290 ScanObject(obj);
1291 fifo[fifo_pos & fifo_mask] = NULL;
1292 --fifo_count;
1293 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001294
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001295 if (!mark_stack_->IsEmpty()) {
1296 const Object* obj = mark_stack_->PopBack();
1297 DCHECK(obj != NULL);
1298 fifo[fifo_pos & fifo_mask] = obj;
1299 __builtin_prefetch(obj);
1300 fifo_count++;
1301 }
1302 fifo_pos++;
1303
1304 if (!fifo_count) {
1305 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
1306 break;
1307 }
1308 }
1309 } else {
1310 while (!mark_stack_->IsEmpty()) {
1311 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001312 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -07001313 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001314 }
1315 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001316}
1317
Carl Shapiro69759ea2011-07-21 18:13:35 -07001318// Walks the reference list marking any references subject to the
1319// reference clearing policy. References with a black referent are
1320// removed from the list. References with white referents biased
1321// toward saving are blackened and also removed from the list.
1322void MarkSweep::PreserveSomeSoftReferences(Object** list) {
1323 DCHECK(list != NULL);
1324 Object* clear = NULL;
1325 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001326
1327 DCHECK(mark_stack_->IsEmpty());
1328
Carl Shapiro69759ea2011-07-21 18:13:35 -07001329 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001330 Object* ref = heap_->DequeuePendingReference(list);
1331 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001332 if (referent == NULL) {
1333 // Referent was cleared by the user during marking.
1334 continue;
1335 }
1336 bool is_marked = IsMarked(referent);
1337 if (!is_marked && ((++counter) & 1)) {
1338 // Referent is white and biased toward saving, mark it.
1339 MarkObject(referent);
1340 is_marked = true;
1341 }
1342 if (!is_marked) {
1343 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001344 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001345 }
1346 }
1347 *list = clear;
1348 // Restart the mark with the newly black references added to the
1349 // root set.
1350 ProcessMarkStack();
1351}
1352
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001353inline bool MarkSweep::IsMarked(const Object* object) const
1354 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
1355 if (object >= immune_begin_ && object < immune_end_) {
1356 return true;
1357 }
1358 DCHECK(current_mark_bitmap_ != NULL);
1359 if (current_mark_bitmap_->HasAddress(object)) {
1360 return current_mark_bitmap_->Test(object);
1361 }
1362 return heap_->GetMarkBitmap()->Test(object);
1363}
1364
1365
Carl Shapiro69759ea2011-07-21 18:13:35 -07001366// Unlink the reference list clearing references objects with white
1367// referents. Cleared references registered to a reference queue are
1368// scheduled for appending by the heap worker thread.
1369void MarkSweep::ClearWhiteReferences(Object** list) {
1370 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001371 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001372 Object* ref = heap_->DequeuePendingReference(list);
1373 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001374 if (referent != NULL && !IsMarked(referent)) {
1375 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001376 heap_->ClearReferenceReferent(ref);
1377 if (heap_->IsEnqueuable(ref)) {
1378 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001379 }
1380 }
1381 }
1382 DCHECK(*list == NULL);
1383}
1384
1385// Enqueues finalizer references with white referents. White
1386// referents are blackened, moved to the zombie field, and the
1387// referent field is cleared.
1388void MarkSweep::EnqueueFinalizerReferences(Object** list) {
1389 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001390 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001391 bool has_enqueued = false;
1392 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001393 Object* ref = heap_->DequeuePendingReference(list);
1394 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001395 if (referent != NULL && !IsMarked(referent)) {
1396 MarkObject(referent);
1397 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001398 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001399 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001400 heap_->ClearReferenceReferent(ref);
1401 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001402 has_enqueued = true;
1403 }
1404 }
1405 if (has_enqueued) {
1406 ProcessMarkStack();
1407 }
1408 DCHECK(*list == NULL);
1409}
1410
Carl Shapiro58551df2011-07-24 03:09:51 -07001411// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001412void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1413 Object** weak_references,
1414 Object** finalizer_references,
1415 Object** phantom_references) {
1416 DCHECK(soft_references != NULL);
1417 DCHECK(weak_references != NULL);
1418 DCHECK(finalizer_references != NULL);
1419 DCHECK(phantom_references != NULL);
1420
1421 // Unless we are in the zygote or required to clear soft references
1422 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001423 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001424 PreserveSomeSoftReferences(soft_references);
1425 }
1426
1427 // Clear all remaining soft and weak references with white
1428 // referents.
1429 ClearWhiteReferences(soft_references);
1430 ClearWhiteReferences(weak_references);
1431
1432 // Preserve all white objects with finalize methods and schedule
1433 // them for finalization.
1434 EnqueueFinalizerReferences(finalizer_references);
1435
1436 // Clear all f-reachable soft and weak references with white
1437 // referents.
1438 ClearWhiteReferences(soft_references);
1439 ClearWhiteReferences(weak_references);
1440
1441 // Clear all phantom references with white referents.
1442 ClearWhiteReferences(phantom_references);
1443
1444 // At this point all reference lists should be empty.
1445 DCHECK(*soft_references == NULL);
1446 DCHECK(*weak_references == NULL);
1447 DCHECK(*finalizer_references == NULL);
1448 DCHECK(*phantom_references == NULL);
1449}
1450
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001451void MarkSweep::UnBindBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -07001452 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1453 // TODO: C++0x
1454 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1455 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1456 space::ContinuousSpace* space = *it;
1457 if (space->IsDlMallocSpace()) {
1458 space::DlMallocSpace* alloc_space = space->AsDlMallocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001459 if (alloc_space->temp_bitmap_.get() != NULL) {
1460 // At this point, the temp_bitmap holds our old mark bitmap.
Ian Rogers1d54e732013-05-02 21:10:01 -07001461 accounting::SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001462 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1463 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1464 alloc_space->mark_bitmap_.reset(new_bitmap);
1465 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1466 }
1467 }
1468 }
1469}
1470
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001471void MarkSweep::FinishPhase() {
1472 // Can't enqueue referneces if we hold the mutator lock.
1473 Object* cleared_references = GetClearedReferences();
Ian Rogers1d54e732013-05-02 21:10:01 -07001474 Heap* heap = GetHeap();
1475 heap->EnqueueClearedReferences(&cleared_references);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001476
Ian Rogers1d54e732013-05-02 21:10:01 -07001477 heap->PostGcVerification(this);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001478
Ian Rogers1d54e732013-05-02 21:10:01 -07001479 timings_.NewSplit("GrowForUtilization");
Mathieu Chartierbdd0fb92013-07-02 10:16:15 -07001480 heap->GrowForUtilization(GetGcType(), GetDurationNs());
Mathieu Chartier65db8802012-11-20 12:36:46 -08001481
Ian Rogers1d54e732013-05-02 21:10:01 -07001482 timings_.NewSplit("RequestHeapTrim");
1483 heap->RequestHeapTrim();
Mathieu Chartier65db8802012-11-20 12:36:46 -08001484
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001485 // Update the cumulative statistics
Ian Rogers1d54e732013-05-02 21:10:01 -07001486 total_time_ns_ += GetDurationNs();
1487 total_paused_time_ns_ += std::accumulate(GetPauseTimes().begin(), GetPauseTimes().end(), 0,
1488 std::plus<uint64_t>());
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001489 total_freed_objects_ += GetFreedObjects();
1490 total_freed_bytes_ += GetFreedBytes();
1491
1492 // Ensure that the mark stack is empty.
1493 CHECK(mark_stack_->IsEmpty());
1494
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001495 if (kCountScannedTypes) {
1496 VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_
1497 << " other=" << other_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001498 }
1499
1500 if (kCountTasks) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001501 VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001502 }
1503
1504 if (kMeasureOverhead) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001505 VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001506 }
1507
1508 if (kProfileLargeObjects) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001509 VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001510 }
1511
1512 if (kCountClassesMarked) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -08001513 VLOG(gc) << "Classes marked " << classes_marked_;
1514 }
1515
1516 if (kCountJavaLangRefs) {
1517 VLOG(gc) << "References scanned " << reference_count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001518 }
1519
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001520 // Update the cumulative loggers.
1521 cumulative_timings_.Start();
Ian Rogers1d54e732013-05-02 21:10:01 -07001522 cumulative_timings_.AddNewLogger(timings_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -08001523 cumulative_timings_.End();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001524
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001525 // Clear all of the spaces' mark bitmaps.
Ian Rogers1d54e732013-05-02 21:10:01 -07001526 const std::vector<space::ContinuousSpace*>& spaces = GetHeap()->GetContinuousSpaces();
1527 // TODO: C++0x
1528 typedef std::vector<space::ContinuousSpace*>::const_iterator It;
1529 for (It it = spaces.begin(), end = spaces.end(); it != end; ++it) {
1530 space::ContinuousSpace* space = *it;
1531 if (space->GetGcRetentionPolicy() != space::kGcRetentionPolicyNeverCollect) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001532 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001533 }
1534 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001535 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001536
1537 // Reset the marked large objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001538 space::LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001539 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001540}
1541
Ian Rogers1d54e732013-05-02 21:10:01 -07001542} // namespace collector
1543} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -07001544} // namespace art