Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "mark_sweep.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 18 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 19 | #include <functional> |
| 20 | #include <numeric> |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 21 | #include <climits> |
| 22 | #include <vector> |
| 23 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 24 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 25 | #include "base/macros.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 26 | #include "base/mutex-inl.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 27 | #include "base/timing_logger.h" |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 28 | #include "card_table.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "card_table-inl.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 30 | #include "heap.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 31 | #include "indirect_reference_table.h" |
| 32 | #include "intern_table.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 33 | #include "jni_internal.h" |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 34 | #include "large_object_space.h" |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 35 | #include "monitor.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "mark_sweep-inl.h" |
| 37 | #include "mirror/class-inl.h" |
| 38 | #include "mirror/class_loader.h" |
| 39 | #include "mirror/dex_cache.h" |
| 40 | #include "mirror/field.h" |
| 41 | #include "mirror/field-inl.h" |
| 42 | #include "mirror/object-inl.h" |
| 43 | #include "mirror/object_array.h" |
| 44 | #include "mirror/object_array-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 45 | #include "runtime.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 46 | #include "space.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | #include "space_bitmap-inl.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 48 | #include "thread.h" |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 49 | #include "thread_list.h" |
Ian Rogers | 0825427 | 2012-10-23 17:49:23 -0700 | [diff] [blame] | 50 | #include "verifier/method_verifier.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 51 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | using namespace art::mirror; |
| 53 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 54 | namespace art { |
| 55 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 56 | // Performance options. |
| 57 | static const bool kParallelMarkStack = true; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 58 | static const bool kDisableFinger = kParallelMarkStack; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 59 | static const bool kUseMarkStackPrefetch = true; |
| 60 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 61 | // Profiling and information flags. |
| 62 | static const bool kCountClassesMarked = false; |
| 63 | static const bool kProfileLargeObjects = false; |
| 64 | static const bool kMeasureOverhead = false; |
| 65 | static const bool kCountTasks = false; |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 66 | static const bool kCountJavaLangRefs = false; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 67 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 68 | class SetFingerVisitor { |
| 69 | public: |
| 70 | SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 71 | |
| 72 | } |
| 73 | |
| 74 | void operator ()(void* finger) const { |
| 75 | mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger)); |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | MarkSweep* const mark_sweep_; |
| 80 | }; |
| 81 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 82 | std::string MarkSweep::GetName() const { |
| 83 | std::ostringstream ss; |
| 84 | ss << (IsConcurrent() ? "Concurrent" : "") << GetGcType(); |
| 85 | return ss.str(); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 86 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 87 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 88 | void MarkSweep::ImmuneSpace(ContinuousSpace* space) { |
| 89 | // Bind live to mark bitmap if necessary. |
| 90 | if (space->GetLiveBitmap() != space->GetMarkBitmap()) { |
| 91 | BindLiveToMarkBitmap(space); |
| 92 | } |
| 93 | |
| 94 | // Add the space to the immune region. |
| 95 | if (immune_begin_ == NULL) { |
| 96 | DCHECK(immune_end_ == NULL); |
| 97 | SetImmuneRange(reinterpret_cast<Object*>(space->Begin()), |
| 98 | reinterpret_cast<Object*>(space->End())); |
| 99 | } else { |
| 100 | const Spaces& spaces = GetHeap()->GetSpaces(); |
| 101 | const ContinuousSpace* prev_space = NULL; |
| 102 | // Find out if the previous space is immune. |
| 103 | // TODO: C++0x |
| 104 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 105 | if (*it == space) { |
| 106 | break; |
| 107 | } |
| 108 | prev_space = *it; |
| 109 | } |
| 110 | |
| 111 | // If previous space was immune, then extend the immune region. |
| 112 | if (prev_space != NULL && |
| 113 | immune_begin_ <= reinterpret_cast<Object*>(prev_space->Begin()) && |
| 114 | immune_end_ >= reinterpret_cast<Object*>(prev_space->End())) { |
| 115 | immune_begin_ = std::min(reinterpret_cast<Object*>(space->Begin()), immune_begin_); |
| 116 | immune_end_ = std::max(reinterpret_cast<Object*>(space->End()), immune_end_); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Bind the live bits to the mark bits of bitmaps based on the gc type. |
| 122 | void MarkSweep::BindBitmaps() { |
| 123 | Spaces& spaces = GetHeap()->GetSpaces(); |
| 124 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 125 | |
| 126 | // Mark all of the spaces we never collect as immune. |
| 127 | for (Spaces::iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 128 | ContinuousSpace* space = *it; |
| 129 | if (space->GetGcRetentionPolicy() == kGcRetentionPolicyNeverCollect) { |
| 130 | ImmuneSpace(space); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | MarkSweep::MarkSweep(Heap* heap, bool is_concurrent) |
| 136 | : GarbageCollector(heap), |
| 137 | gc_barrier_(new Barrier(0)), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 138 | large_object_lock_("mark sweep large object lock", kMarkSweepLargeObjectLock), |
| 139 | mark_stack_expand_lock_("mark sweep mark stack expand lock"), |
Ian Rogers | 1bd4b4c | 2013-04-18 17:47:42 -0700 | [diff] [blame] | 140 | is_concurrent_(is_concurrent), |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 141 | timings_(GetName(), true), |
Ian Rogers | 1bd4b4c | 2013-04-18 17:47:42 -0700 | [diff] [blame] | 142 | cumulative_timings_(GetName()) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 143 | cumulative_timings_.SetName(GetName()); |
| 144 | ResetCumulativeStatistics(); |
| 145 | } |
| 146 | |
| 147 | void MarkSweep::InitializePhase() { |
| 148 | mark_stack_ = GetHeap()->mark_stack_.get(); |
| 149 | DCHECK(mark_stack_ != NULL); |
| 150 | finger_ = NULL; |
| 151 | SetImmuneRange(NULL, NULL); |
| 152 | soft_reference_list_ = NULL; |
| 153 | weak_reference_list_ = NULL; |
| 154 | finalizer_reference_list_ = NULL; |
| 155 | phantom_reference_list_ = NULL; |
| 156 | cleared_reference_list_ = NULL; |
| 157 | freed_bytes_ = 0; |
| 158 | freed_objects_ = 0; |
| 159 | class_count_ = 0; |
| 160 | array_count_ = 0; |
| 161 | other_count_ = 0; |
| 162 | large_object_test_ = 0; |
| 163 | large_object_mark_ = 0; |
| 164 | classes_marked_ = 0; |
| 165 | overhead_time_ = 0; |
| 166 | work_chunks_created_ = 0; |
| 167 | work_chunks_deleted_ = 0; |
| 168 | reference_count_ = 0; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 169 | java_lang_Class_ = Class::GetJavaLangClass(); |
| 170 | CHECK(java_lang_Class_ != NULL); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 171 | FindDefaultMarkBitmap(); |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 172 | // Mark any concurrent roots as dirty since we need to scan them at least once during this GC. |
| 173 | Runtime::Current()->DirtyRoots(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 174 | timings_.Reset(); |
| 175 | // Do any pre GC verification. |
| 176 | heap_->PreGcVerification(this); |
| 177 | } |
| 178 | |
| 179 | void MarkSweep::ProcessReferences(Thread* self) { |
Mathieu Chartier | 8e56c7e | 2012-11-20 13:25:50 -0800 | [diff] [blame] | 180 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 181 | ProcessReferences(&soft_reference_list_, clear_soft_references_, &weak_reference_list_, |
| 182 | &finalizer_reference_list_, &phantom_reference_list_); |
| 183 | timings_.AddSplit("ProcessReferences"); |
| 184 | } |
| 185 | |
| 186 | bool MarkSweep::HandleDirtyObjectsPhase() { |
| 187 | Thread* self = Thread::Current(); |
| 188 | ObjectStack* allocation_stack = GetHeap()->allocation_stack_.get(); |
| 189 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 190 | |
| 191 | { |
| 192 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 193 | |
| 194 | // Re-mark root set. |
| 195 | ReMarkRoots(); |
| 196 | timings_.AddSplit("ReMarkRoots"); |
| 197 | |
| 198 | // Scan dirty objects, this is only required if we are not doing concurrent GC. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 199 | RecursiveMarkDirtyObjects(CardTable::kCardDirty); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | ProcessReferences(self); |
| 203 | |
| 204 | // Only need to do this if we have the card mark verification on, and only during concurrent GC. |
| 205 | if (GetHeap()->verify_missing_card_marks_) { |
| 206 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 207 | // This second sweep makes sure that we don't have any objects in the live stack which point to |
| 208 | // freed objects. These cause problems since their references may be previously freed objects. |
| 209 | SweepArray(timings_, allocation_stack, false); |
| 210 | } else { |
| 211 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 212 | // We only sweep over the live stack, and the live stack should not intersect with the |
| 213 | // allocation stack, so it should be safe to UnMark anything in the allocation stack as live. |
| 214 | heap_->UnMarkAllocStack(GetHeap()->alloc_space_->GetMarkBitmap(), |
| 215 | GetHeap()->large_object_space_->GetMarkObjects(), |
| 216 | allocation_stack); |
| 217 | timings_.AddSplit("UnMarkAllocStack"); |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | bool MarkSweep::IsConcurrent() const { |
| 223 | return is_concurrent_; |
| 224 | } |
| 225 | |
| 226 | void MarkSweep::MarkingPhase() { |
| 227 | Heap* heap = GetHeap(); |
| 228 | Thread* self = Thread::Current(); |
| 229 | |
| 230 | BindBitmaps(); |
| 231 | FindDefaultMarkBitmap(); |
| 232 | timings_.AddSplit("BindBitmaps"); |
| 233 | |
| 234 | // Process dirty cards and add dirty cards to mod union tables. |
| 235 | heap->ProcessCards(timings_); |
| 236 | |
| 237 | // Need to do this before the checkpoint since we don't want any threads to add references to |
| 238 | // the live stack during the recursive mark. |
| 239 | heap->SwapStacks(); |
| 240 | timings_.AddSplit("SwapStacks"); |
| 241 | |
| 242 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 243 | if (Locks::mutator_lock_->IsExclusiveHeld(self)) { |
| 244 | // If we exclusively hold the mutator lock, all threads must be suspended. |
| 245 | MarkRoots(); |
| 246 | timings_.AddSplit("MarkConcurrentRoots"); |
| 247 | } else { |
| 248 | MarkRootsCheckpoint(); |
| 249 | timings_.AddSplit("MarkRootsCheckpoint"); |
| 250 | MarkNonThreadRoots(); |
| 251 | timings_.AddSplit("MarkNonThreadRoots"); |
| 252 | } |
| 253 | MarkConcurrentRoots(); |
| 254 | timings_.AddSplit("MarkConcurrentRoots"); |
| 255 | |
| 256 | heap->UpdateAndMarkModUnion(this, timings_, GetGcType()); |
| 257 | MarkReachableObjects(); |
| 258 | } |
| 259 | |
| 260 | void MarkSweep::MarkReachableObjects() { |
| 261 | // Mark everything allocated since the last as GC live so that we can sweep concurrently, |
| 262 | // knowing that new allocations won't be marked as live. |
| 263 | ObjectStack* live_stack = heap_->GetLiveStack(); |
| 264 | heap_->MarkAllocStack(heap_->alloc_space_->GetLiveBitmap(), |
| 265 | heap_->large_object_space_->GetLiveObjects(), |
| 266 | live_stack); |
| 267 | live_stack->Reset(); |
| 268 | timings_.AddSplit("MarkStackAsLive"); |
| 269 | // Recursively mark all the non-image bits set in the mark bitmap. |
| 270 | RecursiveMark(); |
| 271 | DisableFinger(); |
| 272 | } |
| 273 | |
| 274 | void MarkSweep::ReclaimPhase() { |
| 275 | Thread* self = Thread::Current(); |
| 276 | |
| 277 | if (!IsConcurrent()) { |
| 278 | ProcessReferences(self); |
| 279 | } |
| 280 | |
| 281 | // Before freeing anything, lets verify the heap. |
| 282 | if (kIsDebugBuild) { |
| 283 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 284 | VerifyImageRoots(); |
| 285 | } |
| 286 | heap_->PreSweepingGcVerification(this); |
| 287 | |
| 288 | { |
| 289 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 290 | |
| 291 | // Reclaim unmarked objects. |
| 292 | Sweep(timings_, false); |
| 293 | |
| 294 | // Swap the live and mark bitmaps for each space which we modified space. This is an |
| 295 | // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound |
| 296 | // bitmaps. |
| 297 | SwapBitmaps(); |
| 298 | timings_.AddSplit("SwapBitmaps"); |
| 299 | |
| 300 | // Unbind the live and mark bitmaps. |
| 301 | UnBindBitmaps(); |
| 302 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void MarkSweep::SwapBitmaps() { |
| 306 | // Swap the live and mark bitmaps for each alloc space. This is needed since sweep re-swaps |
| 307 | // these bitmaps. The bitmap swapping is an optimization so that we do not need to clear the live |
| 308 | // bits of dead objects in the live bitmap. |
| 309 | const GcType gc_type = GetGcType(); |
| 310 | // TODO: C++0x |
| 311 | Spaces& spaces = heap_->GetSpaces(); |
| 312 | for (Spaces::iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 313 | ContinuousSpace* space = *it; |
| 314 | // We never allocate into zygote spaces. |
| 315 | if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect || |
| 316 | (gc_type == kGcTypeFull && |
| 317 | space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)) { |
| 318 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 319 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 320 | if (live_bitmap != mark_bitmap) { |
| 321 | heap_->GetLiveBitmap()->ReplaceBitmap(live_bitmap, mark_bitmap); |
| 322 | heap_->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap); |
| 323 | space->AsAllocSpace()->SwapBitmaps(); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | SwapLargeObjects(); |
| 328 | } |
| 329 | |
| 330 | void MarkSweep::SwapLargeObjects() { |
| 331 | LargeObjectSpace* large_object_space = heap_->GetLargeObjectsSpace(); |
| 332 | large_object_space->SwapBitmaps(); |
| 333 | heap_->GetLiveBitmap()->SetLargeObjects(large_object_space->GetLiveObjects()); |
| 334 | heap_->GetMarkBitmap()->SetLargeObjects(large_object_space->GetMarkObjects()); |
| 335 | } |
| 336 | |
| 337 | void MarkSweep::SetImmuneRange(Object* begin, Object* end) { |
| 338 | immune_begin_ = begin; |
| 339 | immune_end_ = end; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 342 | void MarkSweep::FindDefaultMarkBitmap() { |
| 343 | const Spaces& spaces = heap_->GetSpaces(); |
| 344 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 345 | if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) { |
| 346 | current_mark_bitmap_ = (*it)->GetMarkBitmap(); |
| 347 | CHECK(current_mark_bitmap_ != NULL); |
| 348 | return; |
| 349 | } |
| 350 | } |
| 351 | GetHeap()->DumpSpaces(); |
| 352 | LOG(FATAL) << "Could not find a default mark bitmap"; |
| 353 | } |
| 354 | |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 355 | void MarkSweep::ExpandMarkStack() { |
| 356 | // Rare case, no need to have Thread::Current be a parameter. |
| 357 | MutexLock mu(Thread::Current(), mark_stack_expand_lock_); |
| 358 | if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) { |
| 359 | // Someone else acquired the lock and expanded the mark stack before us. |
| 360 | return; |
| 361 | } |
| 362 | std::vector<Object*> temp; |
| 363 | temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End()); |
| 364 | mark_stack_->Resize(mark_stack_->Capacity() * 2); |
| 365 | for (size_t i = 0; i < temp.size(); ++i) { |
| 366 | mark_stack_->PushBack(temp[i]); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | inline void MarkSweep::MarkObjectNonNullParallel(const Object* obj, bool check_finger) { |
| 371 | DCHECK(obj != NULL); |
| 372 | if (MarkObjectParallel(obj)) { |
| 373 | if (kDisableFinger || (check_finger && obj < finger_)) { |
| 374 | while (UNLIKELY(!mark_stack_->AtomicPushBack(const_cast<Object*>(obj)))) { |
| 375 | // Only reason a push can fail is that the mark stack is full. |
| 376 | ExpandMarkStack(); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 382 | inline void MarkSweep::MarkObjectNonNull(const Object* obj, bool check_finger) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 383 | DCHECK(obj != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 384 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 385 | if (obj >= immune_begin_ && obj < immune_end_) { |
| 386 | DCHECK(IsMarked(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 387 | return; |
| 388 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 389 | |
| 390 | // Try to take advantage of locality of references within a space, failing this find the space |
| 391 | // the hard way. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 392 | SpaceBitmap* object_bitmap = current_mark_bitmap_; |
| 393 | if (UNLIKELY(!object_bitmap->HasAddress(obj))) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 394 | SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj); |
| 395 | if (new_bitmap != NULL) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 396 | object_bitmap = new_bitmap; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 397 | } else { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 398 | MarkLargeObject(obj); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 399 | return; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 400 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 403 | // This object was not previously marked. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 404 | if (!object_bitmap->Test(obj)) { |
| 405 | object_bitmap->Set(obj); |
| 406 | if (kDisableFinger || (check_finger && obj < finger_)) { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 407 | // Do we need to expand the mark stack? |
| 408 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 409 | ExpandMarkStack(); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 410 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 411 | // The object must be pushed on to the mark stack. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 412 | mark_stack_->PushBack(const_cast<Object*>(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 417 | // Rare case, probably not worth inlining since it will increase instruction cache miss rate. |
| 418 | bool MarkSweep::MarkLargeObject(const Object* obj) { |
| 419 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 420 | SpaceSetMap* large_objects = large_object_space->GetMarkObjects(); |
| 421 | if (kProfileLargeObjects) { |
| 422 | ++large_object_test_; |
| 423 | } |
| 424 | if (UNLIKELY(!large_objects->Test(obj))) { |
Ian Rogers | 8afe6e0 | 2013-06-12 19:40:25 -0700 | [diff] [blame^] | 425 | // TODO: mark may be called holding the JNI global references lock, Contains will hold the |
| 426 | // large object space lock causing a lock level violation. Bug: 9414652; |
| 427 | if (!kDebugLocking && !large_object_space->Contains(obj)) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 428 | LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces"; |
| 429 | LOG(ERROR) << "Attempting see if it's a bad root"; |
| 430 | VerifyRoots(); |
| 431 | LOG(FATAL) << "Can't mark bad root"; |
| 432 | } |
| 433 | if (kProfileLargeObjects) { |
| 434 | ++large_object_mark_; |
| 435 | } |
| 436 | large_objects->Set(obj); |
| 437 | // Don't need to check finger since large objects never have any object references. |
| 438 | return true; |
| 439 | } |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | inline bool MarkSweep::MarkObjectParallel(const Object* obj) { |
| 444 | DCHECK(obj != NULL); |
| 445 | |
| 446 | if (obj >= immune_begin_ && obj < immune_end_) { |
| 447 | DCHECK(IsMarked(obj)); |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | // Try to take advantage of locality of references within a space, failing this find the space |
| 452 | // the hard way. |
| 453 | SpaceBitmap* object_bitmap = current_mark_bitmap_; |
| 454 | if (UNLIKELY(!object_bitmap->HasAddress(obj))) { |
| 455 | SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj); |
| 456 | if (new_bitmap != NULL) { |
| 457 | object_bitmap = new_bitmap; |
| 458 | } else { |
| 459 | // TODO: Remove the Thread::Current here? |
| 460 | // TODO: Convert this to some kind of atomic marking? |
| 461 | MutexLock mu(Thread::Current(), large_object_lock_); |
| 462 | return MarkLargeObject(obj); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // Return true if the object was not previously marked. |
| 467 | return !object_bitmap->AtomicTestAndSet(obj); |
| 468 | } |
| 469 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 470 | // Used to mark objects when recursing. Recursion is done by moving |
| 471 | // the finger across the bitmaps in address order and marking child |
| 472 | // objects. Any newly-marked objects whose addresses are lower than |
| 473 | // the finger won't be visited by the bitmap scan, so those objects |
| 474 | // need to be added to the mark stack. |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 475 | void MarkSweep::MarkObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 476 | if (obj != NULL) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 477 | MarkObjectNonNull(obj, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 481 | void MarkSweep::MarkRoot(const Object* obj) { |
| 482 | if (obj != NULL) { |
| 483 | MarkObjectNonNull(obj, false); |
| 484 | } |
| 485 | } |
| 486 | |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 487 | void MarkSweep::MarkRootParallelCallback(const Object* root, void* arg) { |
| 488 | DCHECK(root != NULL); |
| 489 | DCHECK(arg != NULL); |
| 490 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 491 | mark_sweep->MarkObjectNonNullParallel(root, false); |
| 492 | } |
| 493 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 494 | void MarkSweep::MarkObjectCallback(const Object* root, void* arg) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 495 | DCHECK(root != NULL); |
| 496 | DCHECK(arg != NULL); |
| 497 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 498 | mark_sweep->MarkObjectNonNull(root, false); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 501 | void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) { |
| 502 | DCHECK(root != NULL); |
| 503 | DCHECK(arg != NULL); |
| 504 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 505 | mark_sweep->MarkObjectNonNull(root, true); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 508 | void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg, |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 509 | const StackVisitor* visitor) { |
| 510 | reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 513 | void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 514 | // See if the root is on any space bitmap. |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 515 | if (GetHeap()->GetLiveBitmap()->GetSpaceBitmap(root) == NULL) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 516 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | 4202b74 | 2012-10-17 17:51:25 -0700 | [diff] [blame] | 517 | if (!large_object_space->Contains(root)) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 518 | LOG(ERROR) << "Found invalid root: " << root; |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 519 | if (visitor != NULL) { |
| 520 | LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg; |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void MarkSweep::VerifyRoots() { |
| 527 | Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this); |
| 528 | } |
| 529 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 530 | // Marks all objects in the root set. |
| 531 | void MarkSweep::MarkRoots() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 532 | Runtime::Current()->VisitNonConcurrentRoots(MarkObjectCallback, this); |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 535 | void MarkSweep::MarkNonThreadRoots() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 536 | Runtime::Current()->VisitNonThreadRoots(MarkObjectCallback, this); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 539 | void MarkSweep::MarkConcurrentRoots() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 540 | Runtime::Current()->VisitConcurrentRoots(MarkObjectCallback, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 543 | class CheckObjectVisitor { |
| 544 | public: |
| 545 | CheckObjectVisitor(MarkSweep* const mark_sweep) |
| 546 | : mark_sweep_(mark_sweep) { |
| 547 | |
| 548 | } |
| 549 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 550 | void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 551 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 552 | if (kDebugLocking) { |
| 553 | Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current()); |
| 554 | } |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 555 | mark_sweep_->CheckReference(obj, ref, offset, is_static); |
| 556 | } |
| 557 | |
| 558 | private: |
| 559 | MarkSweep* const mark_sweep_; |
| 560 | }; |
| 561 | |
| 562 | void MarkSweep::CheckObject(const Object* obj) { |
| 563 | DCHECK(obj != NULL); |
| 564 | CheckObjectVisitor visitor(this); |
| 565 | VisitObjectReferences(obj, visitor); |
| 566 | } |
| 567 | |
| 568 | void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) { |
| 569 | DCHECK(root != NULL); |
| 570 | DCHECK(arg != NULL); |
| 571 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 572 | DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 573 | mark_sweep->CheckObject(root); |
| 574 | } |
| 575 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 576 | void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) { |
| 577 | CHECK(space->IsAllocSpace()); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 578 | DlMallocSpace* alloc_space = space->AsAllocSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 579 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 580 | SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release(); |
| 581 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap); |
| 582 | alloc_space->temp_bitmap_.reset(mark_bitmap); |
| 583 | alloc_space->mark_bitmap_.reset(live_bitmap); |
| 584 | } |
| 585 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 586 | class ScanObjectVisitor { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 587 | public: |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 588 | ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 589 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 592 | // TODO: Fixme when anotatalysis works with visitors. |
| 593 | void operator ()(const Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
| 594 | if (kDebugLocking) { |
| 595 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 596 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 597 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 598 | mark_sweep_->ScanObject(obj); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | private: |
| 602 | MarkSweep* const mark_sweep_; |
| 603 | }; |
| 604 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 605 | void MarkSweep::ScanGrayObjects(byte minimum_age) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 606 | const Spaces& spaces = heap_->GetSpaces(); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 607 | CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 608 | ScanObjectVisitor visitor(this); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 609 | SetFingerVisitor finger_visitor(this); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 610 | // TODO: C++ 0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 611 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 612 | ContinuousSpace* space = *it; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 613 | byte* begin = space->Begin(); |
| 614 | byte* end = space->End(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 615 | // Image spaces are handled properly since live == marked for them. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 616 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 617 | card_table->Scan(mark_bitmap, begin, end, visitor, VoidFunctor(), minimum_age); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 621 | class CheckBitmapVisitor { |
| 622 | public: |
| 623 | CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) { |
| 624 | |
| 625 | } |
| 626 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 627 | void operator ()(const Object* obj) const |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 628 | NO_THREAD_SAFETY_ANALYSIS { |
| 629 | if (kDebugLocking) { |
| 630 | Locks::heap_bitmap_lock_->AssertSharedHeld(Thread::Current()); |
| 631 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 632 | DCHECK(obj != NULL); |
| 633 | mark_sweep_->CheckObject(obj); |
| 634 | } |
| 635 | |
| 636 | private: |
| 637 | MarkSweep* mark_sweep_; |
| 638 | }; |
| 639 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 640 | void MarkSweep::VerifyImageRoots() { |
| 641 | // Verify roots ensures that all the references inside the image space point |
| 642 | // objects which are either in the image space or marked objects in the alloc |
| 643 | // space |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 644 | CheckBitmapVisitor visitor(this); |
| 645 | const Spaces& spaces = heap_->GetSpaces(); |
| 646 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 647 | if ((*it)->IsImageSpace()) { |
| 648 | ImageSpace* space = (*it)->AsImageSpace(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 649 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 650 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 651 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 652 | DCHECK(live_bitmap != NULL); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 653 | live_bitmap->VisitMarkedRange(begin, end, visitor, VoidFunctor()); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 658 | // Populates the mark stack based on the set of marked objects and |
| 659 | // recursively marks until the mark stack is emptied. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 660 | void MarkSweep::RecursiveMark() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 661 | // RecursiveMark will build the lists of known instances of the Reference classes. |
| 662 | // See DelayReferenceReferent for details. |
| 663 | CHECK(soft_reference_list_ == NULL); |
| 664 | CHECK(weak_reference_list_ == NULL); |
| 665 | CHECK(finalizer_reference_list_ == NULL); |
| 666 | CHECK(phantom_reference_list_ == NULL); |
| 667 | CHECK(cleared_reference_list_ == NULL); |
| 668 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 669 | const bool partial = GetGcType() == kGcTypePartial; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 670 | const Spaces& spaces = heap_->GetSpaces(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 671 | SetFingerVisitor set_finger_visitor(this); |
| 672 | ScanObjectVisitor scan_visitor(this); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 673 | if (!kDisableFinger) { |
| 674 | finger_ = NULL; |
| 675 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 676 | ContinuousSpace* space = *it; |
| 677 | if ((space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) || |
| 678 | (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) |
| 679 | ) { |
| 680 | current_mark_bitmap_ = space->GetMarkBitmap(); |
| 681 | if (current_mark_bitmap_ == NULL) { |
| 682 | GetHeap()->DumpSpaces(); |
| 683 | LOG(FATAL) << "invalid bitmap"; |
| 684 | } |
| 685 | // This function does not handle heap end increasing, so we must use the space end. |
| 686 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 687 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 688 | current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 689 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 690 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 691 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 692 | DisableFinger(); |
| 693 | timings_.AddSplit("RecursiveMark"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 694 | ProcessMarkStack(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 695 | timings_.AddSplit("ProcessMarkStack"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) { |
| 699 | return |
| 700 | reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) || |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 701 | !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object); |
| 702 | } |
| 703 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 704 | void MarkSweep::RecursiveMarkDirtyObjects(byte minimum_age) { |
| 705 | ScanGrayObjects(minimum_age); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 706 | timings_.AddSplit("ScanGrayObjects"); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 707 | ProcessMarkStack(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 708 | timings_.AddSplit("ProcessMarkStack"); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 711 | void MarkSweep::ReMarkRoots() { |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 712 | Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 715 | void MarkSweep::SweepJniWeakGlobals(IsMarkedTester is_marked, void* arg) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 716 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 717 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 718 | IndirectReferenceTable* table = &vm->weak_globals; |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 719 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 720 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 721 | const Object** entry = *it; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 722 | if (!is_marked(*entry, arg)) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 723 | *entry = kClearedJniWeakGlobal; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 728 | struct ArrayMarkedCheck { |
| 729 | ObjectStack* live_stack; |
| 730 | MarkSweep* mark_sweep; |
| 731 | }; |
| 732 | |
| 733 | // Either marked or not live. |
| 734 | bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) { |
| 735 | ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg); |
| 736 | if (array_check->mark_sweep->IsMarked(object)) { |
| 737 | return true; |
| 738 | } |
| 739 | ObjectStack* live_stack = array_check->live_stack; |
| 740 | return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End(); |
| 741 | } |
| 742 | |
| 743 | void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) { |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 744 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 745 | // The callbacks check |
| 746 | // !is_marked where is_marked is the callback but we want |
| 747 | // !IsMarked && IsLive |
| 748 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 749 | // Or for swapped (IsLive || !IsMarked). |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 750 | |
| 751 | ArrayMarkedCheck visitor; |
| 752 | visitor.live_stack = allocations; |
| 753 | visitor.mark_sweep = this; |
| 754 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor); |
| 755 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor); |
| 756 | SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor); |
| 757 | } |
| 758 | |
| 759 | void MarkSweep::SweepSystemWeaks() { |
| 760 | Runtime* runtime = Runtime::Current(); |
| 761 | // The callbacks check |
| 762 | // !is_marked where is_marked is the callback but we want |
| 763 | // !IsMarked && IsLive |
| 764 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 765 | // Or for swapped (IsLive || !IsMarked). |
| 766 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this); |
| 767 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this); |
| 768 | SweepJniWeakGlobals(IsMarkedCallback, this); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 771 | bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) { |
| 772 | reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj); |
| 773 | // We don't actually want to sweep the object, so lets return "marked" |
| 774 | return true; |
| 775 | } |
| 776 | |
| 777 | void MarkSweep::VerifyIsLive(const Object* obj) { |
| 778 | Heap* heap = GetHeap(); |
| 779 | if (!heap->GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 780 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 781 | if (!large_object_space->GetLiveObjects()->Test(obj)) { |
| 782 | if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) == |
| 783 | heap->allocation_stack_->End()) { |
| 784 | // Object not found! |
| 785 | heap->DumpSpaces(); |
| 786 | LOG(FATAL) << "Found dead object " << obj; |
| 787 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | void MarkSweep::VerifySystemWeaks() { |
| 793 | Runtime* runtime = Runtime::Current(); |
| 794 | // Verify system weaks, uses a special IsMarked callback which always returns true. |
| 795 | runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this); |
| 796 | runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this); |
| 797 | |
| 798 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 799 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 800 | IndirectReferenceTable* table = &vm->weak_globals; |
| 801 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 802 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 803 | const Object** entry = *it; |
| 804 | VerifyIsLive(*entry); |
| 805 | } |
| 806 | } |
| 807 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 808 | struct SweepCallbackContext { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 809 | MarkSweep* mark_sweep; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 810 | AllocSpace* space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 811 | Thread* self; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 812 | }; |
| 813 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 814 | class CheckpointMarkThreadRoots : public Closure { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 815 | public: |
| 816 | CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) { |
| 817 | |
| 818 | } |
| 819 | |
| 820 | virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS { |
| 821 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 822 | Thread* self = Thread::Current(); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 823 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 824 | << thread->GetState() << " thread " << thread << " self " << self; |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 825 | thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 826 | mark_sweep_->GetBarrier().Pass(self); |
| 827 | } |
| 828 | |
| 829 | private: |
| 830 | MarkSweep* mark_sweep_; |
| 831 | }; |
| 832 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 833 | void MarkSweep::ResetCumulativeStatistics() { |
| 834 | cumulative_timings_.Reset(); |
| 835 | total_time_ = 0; |
| 836 | total_paused_time_ = 0; |
| 837 | total_freed_objects_ = 0; |
| 838 | total_freed_bytes_ = 0; |
| 839 | } |
| 840 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 841 | void MarkSweep::MarkRootsCheckpoint() { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 842 | CheckpointMarkThreadRoots check_point(this); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 843 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 844 | // Increment the count of the barrier. If all of the checkpoints have already been finished then |
| 845 | // will hit 0 and continue. Otherwise we are still waiting for some checkpoints, so the counter |
| 846 | // will go positive and we will unblock when it hits zero. |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 847 | gc_barrier_->Increment(Thread::Current(), thread_list->RunCheckpoint(&check_point)); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 850 | void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 851 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 852 | MarkSweep* mark_sweep = context->mark_sweep; |
| 853 | Heap* heap = mark_sweep->GetHeap(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 854 | AllocSpace* space = context->space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 855 | Thread* self = context->self; |
| 856 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 857 | // Use a bulk free, that merges consecutive objects before freeing or free per object? |
| 858 | // Documentation suggests better free performance with merging, but this may be at the expensive |
| 859 | // of allocation. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 860 | size_t freed_objects = num_ptrs; |
| 861 | // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit |
| 862 | size_t freed_bytes = space->FreeList(self, num_ptrs, ptrs); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 863 | heap->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 864 | mark_sweep->freed_objects_ += freed_objects; |
| 865 | mark_sweep->freed_bytes_ += freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 868 | void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 869 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 870 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 871 | Heap* heap = context->mark_sweep->GetHeap(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 872 | // We don't free any actual memory to avoid dirtying the shared zygote pages. |
| 873 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 874 | Object* obj = static_cast<Object*>(ptrs[i]); |
| 875 | heap->GetLiveBitmap()->Clear(obj); |
| 876 | heap->GetCardTable()->MarkCard(obj); |
| 877 | } |
| 878 | } |
| 879 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 880 | void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 881 | size_t freed_bytes = 0; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 882 | DlMallocSpace* space = heap_->GetAllocSpace(); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 883 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 884 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 885 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 886 | SweepSystemWeaksArray(allocations); |
| 887 | logger.AddSplit("SweepSystemWeaks"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 888 | |
| 889 | // Newly allocated objects MUST be in the alloc space and those are the only objects which we are |
| 890 | // going to free. |
| 891 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 892 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 893 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 894 | SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 895 | SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 896 | if (swap_bitmaps) { |
| 897 | std::swap(live_bitmap, mark_bitmap); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 898 | std::swap(large_live_objects, large_mark_objects); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 901 | size_t freed_large_objects = 0; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 902 | size_t count = allocations->Size(); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 903 | Object** objects = const_cast<Object**>(allocations->Begin()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 904 | Object** out = objects; |
| 905 | |
| 906 | // Empty the allocation stack. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 907 | Thread* self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 908 | for (size_t i = 0;i < count;++i) { |
| 909 | Object* obj = objects[i]; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 910 | // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack. |
| 911 | if (LIKELY(mark_bitmap->HasAddress(obj))) { |
| 912 | if (!mark_bitmap->Test(obj)) { |
| 913 | // Don't bother un-marking since we clear the mark bitmap anyways. |
| 914 | *(out++) = obj; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 915 | } |
| 916 | } else if (!large_mark_objects->Test(obj)) { |
| 917 | ++freed_large_objects; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 918 | freed_bytes += large_object_space->Free(self, obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | logger.AddSplit("Process allocation stack"); |
| 922 | |
| 923 | size_t freed_objects = out - objects; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 924 | freed_bytes += space->FreeList(self, freed_objects, objects); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 925 | VLOG(heap) << "Freed " << freed_objects << "/" << count |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 926 | << " objects with size " << PrettySize(freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 927 | heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 928 | freed_objects_ += freed_objects; |
| 929 | freed_bytes_ += freed_bytes; |
| 930 | logger.AddSplit("FreeList"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 931 | allocations->Reset(); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 932 | logger.AddSplit("ResetStack"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 935 | void MarkSweep::Sweep(TimingLogger& timings, bool swap_bitmaps) { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 936 | DCHECK(mark_stack_->IsEmpty()); |
| 937 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 938 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 939 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 940 | SweepSystemWeaks(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 941 | timings.AddSplit("SweepSystemWeaks"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 942 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 943 | const bool partial = GetGcType() == kGcTypePartial; |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 944 | const Spaces& spaces = heap_->GetSpaces(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 945 | SweepCallbackContext scc; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 946 | scc.mark_sweep = this; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 947 | scc.self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 948 | // TODO: C++0x auto |
| 949 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 950 | ContinuousSpace* space = *it; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 951 | if ( |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 952 | space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect || |
| 953 | (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 954 | ) { |
| 955 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 956 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 957 | scc.space = space->AsAllocSpace(); |
| 958 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 959 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 960 | if (swap_bitmaps) { |
| 961 | std::swap(live_bitmap, mark_bitmap); |
| 962 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 963 | if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 964 | // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 965 | SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 966 | &SweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 967 | } else { |
| 968 | // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 969 | SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 970 | &ZygoteSweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 971 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 972 | } |
| 973 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 974 | timings.AddSplit("Sweep"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 975 | |
| 976 | SweepLargeObjects(swap_bitmaps); |
| 977 | timings.AddSplit("SweepLargeObjects"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 980 | void MarkSweep::SweepLargeObjects(bool swap_bitmaps) { |
| 981 | // Sweep large objects |
| 982 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 983 | SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 984 | SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
| 985 | if (swap_bitmaps) { |
| 986 | std::swap(large_live_objects, large_mark_objects); |
| 987 | } |
| 988 | SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects(); |
| 989 | // O(n*log(n)) but hopefully there are not too many large objects. |
| 990 | size_t freed_objects = 0; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 991 | size_t freed_bytes = 0; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 992 | // TODO: C++0x |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 993 | Thread* self = Thread::Current(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 994 | for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) { |
| 995 | if (!large_mark_objects->Test(*it)) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 996 | freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it)); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 997 | ++freed_objects; |
| 998 | } |
| 999 | } |
| 1000 | freed_objects_ += freed_objects; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1001 | freed_bytes_ += freed_bytes; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1002 | // Large objects don't count towards bytes_allocated. |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1003 | GetHeap()->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1006 | void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1007 | const Spaces& spaces = heap_->GetSpaces(); |
| 1008 | // TODO: C++0x auto |
| 1009 | for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) { |
| 1010 | if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) { |
| 1011 | DCHECK(IsMarked(obj)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1012 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1013 | bool is_marked = IsMarked(ref); |
| 1014 | if (!is_marked) { |
| 1015 | LOG(INFO) << **cur; |
| 1016 | LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) |
| 1017 | << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj) |
| 1018 | << "' (" << reinterpret_cast<const void*>(obj) << ") at offset " |
| 1019 | << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 1020 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1021 | const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 1022 | DCHECK(klass != NULL); |
| 1023 | const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields(); |
| 1024 | DCHECK(fields != NULL); |
| 1025 | bool found = false; |
| 1026 | for (int32_t i = 0; i < fields->GetLength(); ++i) { |
| 1027 | const Field* cur = fields->Get(i); |
| 1028 | if (cur->GetOffset().Int32Value() == offset.Int32Value()) { |
| 1029 | LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur); |
| 1030 | found = true; |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | if (!found) { |
| 1035 | LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value(); |
| 1036 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 1037 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1038 | bool obj_marked = heap_->GetCardTable()->IsDirty(obj); |
| 1039 | if (!obj_marked) { |
| 1040 | LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' " |
| 1041 | << "(" << reinterpret_cast<const void*>(obj) << ") contains references to " |
| 1042 | << "the alloc space, but wasn't card marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 1043 | } |
| 1044 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1045 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1046 | break; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1050 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 1051 | // referent has not yet been marked, put it on the appropriate list in |
| 1052 | // the gcHeap for later processing. |
| 1053 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 1054 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 1055 | Class* klass = obj->GetClass(); |
| 1056 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1057 | DCHECK(klass->IsReferenceClass()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1058 | Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false); |
| 1059 | Object* referent = heap_->GetReferenceReferent(obj); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1060 | if (kCountJavaLangRefs) { |
| 1061 | ++reference_count_; |
| 1062 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1063 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1064 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1065 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1066 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1067 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1068 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1069 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1070 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1071 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1072 | list = &phantom_reference_list_; |
| 1073 | } |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 1074 | DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1075 | // TODO: One lock per list? |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1076 | heap_->EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1080 | void MarkSweep::ScanRoot(const Object* obj) { |
| 1081 | ScanObject(obj); |
| 1082 | } |
| 1083 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1084 | class MarkObjectVisitor { |
| 1085 | public: |
| 1086 | MarkObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 1087 | } |
| 1088 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1089 | // TODO: Fixme when anotatalysis works with visitors. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1090 | void operator ()(const Object* /* obj */, const Object* ref, const MemberOffset& /* offset */, |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1091 | bool /* is_static */) const |
| 1092 | NO_THREAD_SAFETY_ANALYSIS { |
| 1093 | if (kDebugLocking) { |
| 1094 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 1095 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 1096 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1097 | mark_sweep_->MarkObject(ref); |
| 1098 | } |
| 1099 | |
| 1100 | private: |
| 1101 | MarkSweep* const mark_sweep_; |
| 1102 | }; |
| 1103 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1104 | // Scans an object reference. Determines the type of the reference |
| 1105 | // and dispatches to a specialized scanning routine. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1106 | void MarkSweep::ScanObject(const Object* obj) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1107 | MarkObjectVisitor visitor(this); |
| 1108 | ScanObjectVisit(obj, visitor); |
| 1109 | } |
| 1110 | |
| 1111 | class MarkStackChunk : public Task { |
| 1112 | public: |
| 1113 | MarkStackChunk(ThreadPool* thread_pool, MarkSweep* mark_sweep, Object** begin, Object** end) |
| 1114 | : mark_sweep_(mark_sweep), |
| 1115 | thread_pool_(thread_pool), |
| 1116 | index_(0), |
| 1117 | length_(0), |
| 1118 | output_(NULL) { |
| 1119 | length_ = end - begin; |
| 1120 | if (begin != end) { |
| 1121 | // Cost not significant since we only do this for the initial set of mark stack chunks. |
| 1122 | memcpy(data_, begin, length_ * sizeof(*begin)); |
| 1123 | } |
| 1124 | if (kCountTasks) { |
| 1125 | ++mark_sweep_->work_chunks_created_; |
| 1126 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1127 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1128 | |
| 1129 | ~MarkStackChunk() { |
| 1130 | DCHECK(output_ == NULL || output_->length_ == 0); |
| 1131 | DCHECK_GE(index_, length_); |
| 1132 | delete output_; |
| 1133 | if (kCountTasks) { |
| 1134 | ++mark_sweep_->work_chunks_deleted_; |
| 1135 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1136 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1137 | |
| 1138 | MarkSweep* const mark_sweep_; |
| 1139 | ThreadPool* const thread_pool_; |
| 1140 | static const size_t max_size = 1 * KB; |
| 1141 | // Index of which object we are scanning. Only needs to be atomic if we are doing work stealing. |
| 1142 | size_t index_; |
| 1143 | // Input / output mark stack. We add newly marked references to data_ until length reaches |
| 1144 | // max_size. This is an optimization so that less tasks are created. |
| 1145 | // TODO: Investigate using a bounded buffer FIFO. |
| 1146 | Object* data_[max_size]; |
| 1147 | // How many elements in data_ we need to scan. |
| 1148 | size_t length_; |
| 1149 | // Output block, newly marked references get added to the ouput block so that another thread can |
| 1150 | // scan them. |
| 1151 | MarkStackChunk* output_; |
| 1152 | |
| 1153 | class MarkObjectParallelVisitor { |
| 1154 | public: |
| 1155 | MarkObjectParallelVisitor(MarkStackChunk* chunk_task) : chunk_task_(chunk_task) { |
| 1156 | |
| 1157 | } |
| 1158 | |
| 1159 | void operator ()(const Object* /* obj */, const Object* ref, |
| 1160 | const MemberOffset& /* offset */, bool /* is_static */) const { |
| 1161 | if (ref != NULL && chunk_task_->mark_sweep_->MarkObjectParallel(ref)) { |
| 1162 | chunk_task_->MarkStackPush(ref); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | private: |
| 1167 | MarkStackChunk* const chunk_task_; |
| 1168 | }; |
| 1169 | |
| 1170 | // Push an object into the block. |
| 1171 | // Don't need to use atomic ++ since we only one thread is writing to an output block at any |
| 1172 | // given time. |
| 1173 | void Push(Object* obj) { |
| 1174 | data_[length_++] = obj; |
| 1175 | } |
| 1176 | |
| 1177 | void MarkStackPush(const Object* obj) { |
| 1178 | if (static_cast<size_t>(length_) < max_size) { |
| 1179 | Push(const_cast<Object*>(obj)); |
| 1180 | } else { |
| 1181 | // Internal buffer is full, push to a new buffer instead. |
| 1182 | if (UNLIKELY(output_ == NULL)) { |
| 1183 | AllocateOutputChunk(); |
| 1184 | } else if (UNLIKELY(static_cast<size_t>(output_->length_) == max_size)) { |
| 1185 | // Output block is full, queue it up for processing and obtain a new block. |
| 1186 | EnqueueOutput(); |
| 1187 | AllocateOutputChunk(); |
| 1188 | } |
| 1189 | output_->Push(const_cast<Object*>(obj)); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | void ScanObject(Object* obj) { |
| 1194 | mark_sweep_->ScanObjectVisit(obj, MarkObjectParallelVisitor(this)); |
| 1195 | } |
| 1196 | |
| 1197 | void EnqueueOutput() { |
| 1198 | if (output_ != NULL) { |
| 1199 | uint64_t start = 0; |
| 1200 | if (kMeasureOverhead) { |
| 1201 | start = NanoTime(); |
| 1202 | } |
| 1203 | thread_pool_->AddTask(Thread::Current(), output_); |
| 1204 | output_ = NULL; |
| 1205 | if (kMeasureOverhead) { |
| 1206 | mark_sweep_->overhead_time_ += NanoTime() - start; |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | void AllocateOutputChunk() { |
| 1212 | uint64_t start = 0; |
| 1213 | if (kMeasureOverhead) { |
| 1214 | start = NanoTime(); |
| 1215 | } |
| 1216 | output_ = new MarkStackChunk(thread_pool_, mark_sweep_, NULL, NULL); |
| 1217 | if (kMeasureOverhead) { |
| 1218 | mark_sweep_->overhead_time_ += NanoTime() - start; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | void Finalize() { |
| 1223 | EnqueueOutput(); |
| 1224 | delete this; |
| 1225 | } |
| 1226 | |
| 1227 | // Scans all of the objects |
| 1228 | virtual void Run(Thread* self) { |
Brian Carlstrom | d74e41b | 2013-03-24 23:47:01 -0700 | [diff] [blame] | 1229 | size_t index; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1230 | while ((index = index_++) < length_) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1231 | if (kUseMarkStackPrefetch) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1232 | static const size_t prefetch_look_ahead = 1; |
| 1233 | __builtin_prefetch(data_[std::min(index + prefetch_look_ahead, length_ - 1)]); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1234 | } |
| 1235 | Object* obj = data_[index]; |
| 1236 | DCHECK(obj != NULL); |
| 1237 | ScanObject(obj); |
| 1238 | } |
| 1239 | } |
| 1240 | }; |
| 1241 | |
| 1242 | void MarkSweep::ProcessMarkStackParallel() { |
| 1243 | CHECK(kDisableFinger) << "parallel mark stack processing cannot work when finger is enabled"; |
| 1244 | Thread* self = Thread::Current(); |
| 1245 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
| 1246 | // Split the current mark stack up into work tasks. |
| 1247 | const size_t num_threads = thread_pool->GetThreadCount(); |
| 1248 | const size_t stack_size = mark_stack_->Size(); |
| 1249 | const size_t chunk_size = |
| 1250 | std::min((stack_size + num_threads - 1) / num_threads, |
| 1251 | static_cast<size_t>(MarkStackChunk::max_size)); |
| 1252 | size_t index = 0; |
| 1253 | for (size_t i = 0; i < num_threads || index < stack_size; ++i) { |
| 1254 | Object** begin = &mark_stack_->Begin()[std::min(stack_size, index)]; |
| 1255 | Object** end = &mark_stack_->Begin()[std::min(stack_size, index + chunk_size)]; |
| 1256 | index += chunk_size; |
| 1257 | thread_pool->AddTask(self, new MarkStackChunk(thread_pool, this, begin, end)); |
| 1258 | } |
| 1259 | thread_pool->StartWorkers(self); |
| 1260 | mark_stack_->Reset(); |
| 1261 | thread_pool->Wait(self, true); |
| 1262 | //LOG(INFO) << "Idle wait time " << PrettyDuration(thread_pool->GetWaitTime()); |
| 1263 | CHECK_EQ(work_chunks_created_, work_chunks_deleted_) << " some of the work chunks were leaked"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1266 | // Scan anything that's on the mark stack. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1267 | void MarkSweep::ProcessMarkStack() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1268 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
| 1269 | if (kParallelMarkStack && thread_pool != NULL && thread_pool->GetThreadCount() > 0) { |
| 1270 | ProcessMarkStackParallel(); |
| 1271 | return; |
| 1272 | } |
| 1273 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1274 | if (kUseMarkStackPrefetch) { |
| 1275 | const size_t fifo_size = 4; |
| 1276 | const size_t fifo_mask = fifo_size - 1; |
| 1277 | const Object* fifo[fifo_size]; |
| 1278 | for (size_t i = 0;i < fifo_size;++i) { |
| 1279 | fifo[i] = NULL; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1280 | } |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1281 | size_t fifo_pos = 0; |
| 1282 | size_t fifo_count = 0; |
| 1283 | for (;;) { |
| 1284 | const Object* obj = fifo[fifo_pos & fifo_mask]; |
| 1285 | if (obj != NULL) { |
| 1286 | ScanObject(obj); |
| 1287 | fifo[fifo_pos & fifo_mask] = NULL; |
| 1288 | --fifo_count; |
| 1289 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1290 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1291 | if (!mark_stack_->IsEmpty()) { |
| 1292 | const Object* obj = mark_stack_->PopBack(); |
| 1293 | DCHECK(obj != NULL); |
| 1294 | fifo[fifo_pos & fifo_mask] = obj; |
| 1295 | __builtin_prefetch(obj); |
| 1296 | fifo_count++; |
| 1297 | } |
| 1298 | fifo_pos++; |
| 1299 | |
| 1300 | if (!fifo_count) { |
| 1301 | CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size(); |
| 1302 | break; |
| 1303 | } |
| 1304 | } |
| 1305 | } else { |
| 1306 | while (!mark_stack_->IsEmpty()) { |
| 1307 | const Object* obj = mark_stack_->PopBack(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1308 | DCHECK(obj != NULL); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1309 | ScanObject(obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1310 | } |
| 1311 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1314 | // Walks the reference list marking any references subject to the |
| 1315 | // reference clearing policy. References with a black referent are |
| 1316 | // removed from the list. References with white referents biased |
| 1317 | // toward saving are blackened and also removed from the list. |
| 1318 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 1319 | DCHECK(list != NULL); |
| 1320 | Object* clear = NULL; |
| 1321 | size_t counter = 0; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1322 | |
| 1323 | DCHECK(mark_stack_->IsEmpty()); |
| 1324 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1325 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1326 | Object* ref = heap_->DequeuePendingReference(list); |
| 1327 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1328 | if (referent == NULL) { |
| 1329 | // Referent was cleared by the user during marking. |
| 1330 | continue; |
| 1331 | } |
| 1332 | bool is_marked = IsMarked(referent); |
| 1333 | if (!is_marked && ((++counter) & 1)) { |
| 1334 | // Referent is white and biased toward saving, mark it. |
| 1335 | MarkObject(referent); |
| 1336 | is_marked = true; |
| 1337 | } |
| 1338 | if (!is_marked) { |
| 1339 | // Referent is white, queue it for clearing. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1340 | heap_->EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | *list = clear; |
| 1344 | // Restart the mark with the newly black references added to the |
| 1345 | // root set. |
| 1346 | ProcessMarkStack(); |
| 1347 | } |
| 1348 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1349 | inline bool MarkSweep::IsMarked(const Object* object) const |
| 1350 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 1351 | if (object >= immune_begin_ && object < immune_end_) { |
| 1352 | return true; |
| 1353 | } |
| 1354 | DCHECK(current_mark_bitmap_ != NULL); |
| 1355 | if (current_mark_bitmap_->HasAddress(object)) { |
| 1356 | return current_mark_bitmap_->Test(object); |
| 1357 | } |
| 1358 | return heap_->GetMarkBitmap()->Test(object); |
| 1359 | } |
| 1360 | |
| 1361 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1362 | // Unlink the reference list clearing references objects with white |
| 1363 | // referents. Cleared references registered to a reference queue are |
| 1364 | // scheduled for appending by the heap worker thread. |
| 1365 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 1366 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1367 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1368 | Object* ref = heap_->DequeuePendingReference(list); |
| 1369 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1370 | if (referent != NULL && !IsMarked(referent)) { |
| 1371 | // Referent is white, clear it. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1372 | heap_->ClearReferenceReferent(ref); |
| 1373 | if (heap_->IsEnqueuable(ref)) { |
| 1374 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | DCHECK(*list == NULL); |
| 1379 | } |
| 1380 | |
| 1381 | // Enqueues finalizer references with white referents. White |
| 1382 | // referents are blackened, moved to the zombie field, and the |
| 1383 | // referent field is cleared. |
| 1384 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 1385 | DCHECK(list != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1386 | MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1387 | bool has_enqueued = false; |
| 1388 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1389 | Object* ref = heap_->DequeuePendingReference(list); |
| 1390 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1391 | if (referent != NULL && !IsMarked(referent)) { |
| 1392 | MarkObject(referent); |
| 1393 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1394 | DCHECK(heap_->IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1395 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1396 | heap_->ClearReferenceReferent(ref); |
| 1397 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1398 | has_enqueued = true; |
| 1399 | } |
| 1400 | } |
| 1401 | if (has_enqueued) { |
| 1402 | ProcessMarkStack(); |
| 1403 | } |
| 1404 | DCHECK(*list == NULL); |
| 1405 | } |
| 1406 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1407 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1408 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 1409 | Object** weak_references, |
| 1410 | Object** finalizer_references, |
| 1411 | Object** phantom_references) { |
| 1412 | DCHECK(soft_references != NULL); |
| 1413 | DCHECK(weak_references != NULL); |
| 1414 | DCHECK(finalizer_references != NULL); |
| 1415 | DCHECK(phantom_references != NULL); |
| 1416 | |
| 1417 | // Unless we are in the zygote or required to clear soft references |
| 1418 | // with white references, preserve some white referents. |
Ian Rogers | 2945e24 | 2012-06-03 14:45:16 -0700 | [diff] [blame] | 1419 | if (!clear_soft && !Runtime::Current()->IsZygote()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1420 | PreserveSomeSoftReferences(soft_references); |
| 1421 | } |
| 1422 | |
| 1423 | // Clear all remaining soft and weak references with white |
| 1424 | // referents. |
| 1425 | ClearWhiteReferences(soft_references); |
| 1426 | ClearWhiteReferences(weak_references); |
| 1427 | |
| 1428 | // Preserve all white objects with finalize methods and schedule |
| 1429 | // them for finalization. |
| 1430 | EnqueueFinalizerReferences(finalizer_references); |
| 1431 | |
| 1432 | // Clear all f-reachable soft and weak references with white |
| 1433 | // referents. |
| 1434 | ClearWhiteReferences(soft_references); |
| 1435 | ClearWhiteReferences(weak_references); |
| 1436 | |
| 1437 | // Clear all phantom references with white referents. |
| 1438 | ClearWhiteReferences(phantom_references); |
| 1439 | |
| 1440 | // At this point all reference lists should be empty. |
| 1441 | DCHECK(*soft_references == NULL); |
| 1442 | DCHECK(*weak_references == NULL); |
| 1443 | DCHECK(*finalizer_references == NULL); |
| 1444 | DCHECK(*phantom_references == NULL); |
| 1445 | } |
| 1446 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1447 | void MarkSweep::UnBindBitmaps() { |
| 1448 | const Spaces& spaces = heap_->GetSpaces(); |
| 1449 | // TODO: C++0x auto |
| 1450 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 1451 | Space* space = *it; |
| 1452 | if (space->IsAllocSpace()) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 1453 | DlMallocSpace* alloc_space = space->AsAllocSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1454 | if (alloc_space->temp_bitmap_.get() != NULL) { |
| 1455 | // At this point, the temp_bitmap holds our old mark bitmap. |
| 1456 | SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release(); |
| 1457 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap); |
| 1458 | CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get()); |
| 1459 | alloc_space->mark_bitmap_.reset(new_bitmap); |
| 1460 | DCHECK(alloc_space->temp_bitmap_.get() == NULL); |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1466 | void MarkSweep::FinishPhase() { |
| 1467 | // Can't enqueue referneces if we hold the mutator lock. |
| 1468 | Object* cleared_references = GetClearedReferences(); |
| 1469 | heap_->EnqueueClearedReferences(&cleared_references); |
| 1470 | |
| 1471 | heap_->PostGcVerification(this); |
| 1472 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1473 | heap_->GrowForUtilization(GetDuration()); |
| 1474 | timings_.AddSplit("GrowForUtilization"); |
| 1475 | |
| 1476 | heap_->RequestHeapTrim(); |
| 1477 | timings_.AddSplit("RequestHeapTrim"); |
| 1478 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1479 | // Update the cumulative statistics |
| 1480 | total_time_ += GetDuration(); |
| 1481 | total_paused_time_ += std::accumulate(GetPauseTimes().begin(), GetPauseTimes().end(), 0, |
| 1482 | std::plus<uint64_t>()); |
| 1483 | total_freed_objects_ += GetFreedObjects(); |
| 1484 | total_freed_bytes_ += GetFreedBytes(); |
| 1485 | |
| 1486 | // Ensure that the mark stack is empty. |
| 1487 | CHECK(mark_stack_->IsEmpty()); |
| 1488 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1489 | if (kCountScannedTypes) { |
| 1490 | VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ |
| 1491 | << " other=" << other_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | if (kCountTasks) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1495 | VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | if (kMeasureOverhead) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1499 | VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | if (kProfileLargeObjects) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1503 | VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | if (kCountClassesMarked) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1507 | VLOG(gc) << "Classes marked " << classes_marked_; |
| 1508 | } |
| 1509 | |
| 1510 | if (kCountJavaLangRefs) { |
| 1511 | VLOG(gc) << "References scanned " << reference_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1514 | // Update the cumulative loggers. |
| 1515 | cumulative_timings_.Start(); |
| 1516 | cumulative_timings_.AddLogger(timings_); |
| 1517 | cumulative_timings_.End(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1518 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1519 | // Clear all of the spaces' mark bitmaps. |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1520 | const Spaces& spaces = heap_->GetSpaces(); |
| 1521 | // TODO: C++0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1522 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1523 | ContinuousSpace* space = *it; |
| 1524 | if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) { |
| 1525 | space->GetMarkBitmap()->Clear(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1526 | } |
| 1527 | } |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 1528 | mark_stack_->Reset(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1529 | |
| 1530 | // Reset the marked large objects. |
| 1531 | LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1532 | large_objects->GetMarkObjects()->Clear(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1535 | MarkSweep::~MarkSweep() { |
| 1536 | |
| 1537 | } |
| 1538 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1539 | } // namespace art |