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 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 19 | #include <climits> |
| 20 | #include <vector> |
| 21 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame^] | 22 | #include "barrier.h" |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 23 | #include "card_table.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 24 | #include "class_loader.h" |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 25 | #include "dex_cache.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 26 | #include "heap.h" |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 27 | #include "indirect_reference_table.h" |
| 28 | #include "intern_table.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 29 | #include "jni_internal.h" |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 30 | #include "large_object_space.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 31 | #include "logging.h" |
| 32 | #include "macros.h" |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 33 | #include "monitor.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 34 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 35 | #include "runtime.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 36 | #include "space.h" |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 37 | #include "timing_logger.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 38 | #include "thread.h" |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 39 | #include "thread_list.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 40 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 41 | namespace art { |
| 42 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame^] | 43 | static const bool kUseMarkStackPrefetch = true; |
| 44 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 45 | class SetFingerVisitor { |
| 46 | public: |
| 47 | SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 48 | |
| 49 | } |
| 50 | |
| 51 | void operator ()(void* finger) const { |
| 52 | mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger)); |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | MarkSweep* const mark_sweep_; |
| 57 | }; |
| 58 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 59 | MarkSweep::MarkSweep(ObjectStack* mark_stack) |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 60 | : current_mark_bitmap_(NULL), |
| 61 | mark_stack_(mark_stack), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 62 | heap_(NULL), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 63 | finger_(NULL), |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 64 | immune_begin_(NULL), |
| 65 | immune_end_(NULL), |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 66 | soft_reference_list_(NULL), |
| 67 | weak_reference_list_(NULL), |
| 68 | finalizer_reference_list_(NULL), |
| 69 | phantom_reference_list_(NULL), |
| 70 | cleared_reference_list_(NULL), |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 71 | freed_bytes_(0), freed_objects_(0), |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame^] | 72 | class_count_(0), array_count_(0), other_count_(0), |
| 73 | gc_barrier_(new Barrier) { |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 74 | DCHECK(mark_stack_ != NULL); |
| 75 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 76 | |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 77 | void MarkSweep::Init() { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 78 | heap_ = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 79 | mark_stack_->Reset(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 80 | // TODO: C++0x auto |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 81 | FindDefaultMarkBitmap(); |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 82 | // TODO: if concurrent, enable card marking in compiler |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 83 | // TODO: check that the mark bitmap is entirely clear. |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 84 | // Mark any concurrent roots as dirty since we need to scan them at least once during this GC. |
| 85 | Runtime::Current()->DirtyRoots(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 88 | void MarkSweep::FindDefaultMarkBitmap() { |
| 89 | const Spaces& spaces = heap_->GetSpaces(); |
| 90 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 91 | if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) { |
| 92 | current_mark_bitmap_ = (*it)->GetMarkBitmap(); |
| 93 | CHECK(current_mark_bitmap_ != NULL); |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | GetHeap()->DumpSpaces(); |
| 98 | LOG(FATAL) << "Could not find a default mark bitmap"; |
| 99 | } |
| 100 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 101 | inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 102 | DCHECK(obj != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 103 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 104 | if (obj >= immune_begin_ && obj < immune_end_) { |
| 105 | DCHECK(IsMarked(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 106 | return; |
| 107 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 108 | |
| 109 | // Try to take advantage of locality of references within a space, failing this find the space |
| 110 | // the hard way. |
Ian Rogers | 506de0c | 2012-09-17 15:39:06 -0700 | [diff] [blame] | 111 | if (UNLIKELY(!current_mark_bitmap_->HasAddress(obj))) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 112 | SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj); |
| 113 | if (new_bitmap != NULL) { |
| 114 | current_mark_bitmap_ = new_bitmap; |
| 115 | } else { |
| 116 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 117 | SpaceSetMap* large_objects = large_object_space->GetMarkObjects(); |
| 118 | if (!large_objects->Test(obj)) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 119 | if (!large_object_space->Contains(obj)) { |
| 120 | LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces"; |
| 121 | LOG(ERROR) << "Attempting see if it's a bad root"; |
| 122 | VerifyRoots(); |
| 123 | LOG(FATAL) << "Can't mark bad root"; |
| 124 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 125 | large_objects->Set(obj); |
| 126 | // Don't need to check finger since large objects never have any object references. |
| 127 | } |
| 128 | // TODO: Improve clarity of control flow in this function? |
| 129 | return; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 130 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 133 | // This object was not previously marked. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 134 | if (!current_mark_bitmap_->Test(obj)) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 135 | current_mark_bitmap_->Set(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 136 | if (check_finger && obj < finger_) { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 137 | // Do we need to expand the mark stack? |
| 138 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
| 139 | std::vector<Object*> temp; |
| 140 | temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End()); |
| 141 | mark_stack_->Resize(mark_stack_->Capacity() * 2); |
| 142 | for (size_t i = 0; i < temp.size(); ++i) { |
| 143 | mark_stack_->PushBack(temp[i]); |
| 144 | } |
| 145 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 146 | // The object must be pushed on to the mark stack. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 147 | mark_stack_->PushBack(const_cast<Object*>(obj)); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Used to mark objects when recursing. Recursion is done by moving |
| 153 | // the finger across the bitmaps in address order and marking child |
| 154 | // objects. Any newly-marked objects whose addresses are lower than |
| 155 | // the finger won't be visited by the bitmap scan, so those objects |
| 156 | // need to be added to the mark stack. |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 157 | void MarkSweep::MarkObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 158 | if (obj != NULL) { |
| 159 | MarkObject0(obj, true); |
| 160 | } |
| 161 | } |
| 162 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 163 | void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 164 | DCHECK(root != NULL); |
| 165 | DCHECK(arg != NULL); |
| 166 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 167 | mark_sweep->MarkObject0(root, false); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 170 | void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) { |
| 171 | DCHECK(root != NULL); |
| 172 | DCHECK(arg != NULL); |
| 173 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 174 | mark_sweep->MarkObject0(root, true); |
| 175 | } |
| 176 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 177 | void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg, |
| 178 | const AbstractMethod* method) { |
| 179 | reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, method); |
| 180 | } |
| 181 | |
| 182 | void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const AbstractMethod* method) { |
| 183 | // See if the root is on any space bitmap. |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 184 | if (GetHeap()->GetLiveBitmap()->GetSpaceBitmap(root) == NULL) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 185 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | 4202b74 | 2012-10-17 17:51:25 -0700 | [diff] [blame] | 186 | if (!large_object_space->Contains(root)) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 187 | LOG(ERROR) << "Found invalid root: " << root; |
| 188 | LOG(ERROR) << "VReg / Shadow frame offset: " << vreg; |
| 189 | if (method != NULL) { |
| 190 | LOG(ERROR) << "In method " << PrettyMethod(method, true); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void MarkSweep::VerifyRoots() { |
| 197 | Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this); |
| 198 | } |
| 199 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 200 | // Marks all objects in the root set. |
| 201 | void MarkSweep::MarkRoots() { |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 202 | Runtime::Current()->VisitNonConcurrentRoots(MarkObjectVisitor, this); |
| 203 | } |
| 204 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame^] | 205 | void MarkSweep::MarkNonThreadRoots() { |
| 206 | Runtime::Current()->VisitNonThreadRoots(MarkObjectVisitor, this); |
| 207 | } |
| 208 | |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 209 | void MarkSweep::MarkConcurrentRoots() { |
| 210 | Runtime::Current()->VisitConcurrentRoots(MarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 213 | class CheckObjectVisitor { |
| 214 | public: |
| 215 | CheckObjectVisitor(MarkSweep* const mark_sweep) |
| 216 | : mark_sweep_(mark_sweep) { |
| 217 | |
| 218 | } |
| 219 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 220 | void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 221 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 222 | Locks::mutator_lock_) { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 223 | mark_sweep_->CheckReference(obj, ref, offset, is_static); |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | MarkSweep* const mark_sweep_; |
| 228 | }; |
| 229 | |
| 230 | void MarkSweep::CheckObject(const Object* obj) { |
| 231 | DCHECK(obj != NULL); |
| 232 | CheckObjectVisitor visitor(this); |
| 233 | VisitObjectReferences(obj, visitor); |
| 234 | } |
| 235 | |
| 236 | void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) { |
| 237 | DCHECK(root != NULL); |
| 238 | DCHECK(arg != NULL); |
| 239 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 240 | DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 241 | mark_sweep->CheckObject(root); |
| 242 | } |
| 243 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 244 | void MarkSweep::CopyMarkBits(ContinuousSpace* space) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 245 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 246 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 247 | mark_bitmap->CopyFrom(live_bitmap); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 250 | void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) { |
| 251 | CHECK(space->IsAllocSpace()); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 252 | DlMallocSpace* alloc_space = space->AsAllocSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 253 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 254 | SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release(); |
| 255 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap); |
| 256 | alloc_space->temp_bitmap_.reset(mark_bitmap); |
| 257 | alloc_space->mark_bitmap_.reset(live_bitmap); |
| 258 | } |
| 259 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 260 | class ScanImageRootVisitor { |
| 261 | public: |
| 262 | ScanImageRootVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 265 | void operator ()(const Object* root) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 266 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 267 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 268 | DCHECK(root != NULL); |
| 269 | mark_sweep_->ScanObject(root); |
| 270 | } |
| 271 | |
| 272 | private: |
| 273 | MarkSweep* const mark_sweep_; |
| 274 | }; |
| 275 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 276 | void MarkSweep::ScanGrayObjects(bool update_finger) { |
| 277 | const Spaces& spaces = heap_->GetSpaces(); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 278 | CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 279 | ScanImageRootVisitor image_root_visitor(this); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 280 | SetFingerVisitor finger_visitor(this); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 281 | // TODO: C++ 0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 282 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 283 | ContinuousSpace* space = *it; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 284 | byte* begin = space->Begin(); |
| 285 | byte* end = space->End(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 286 | // Image spaces are handled properly since live == marked for them. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 287 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
| 288 | if (update_finger) { |
| 289 | card_table->Scan(mark_bitmap, begin, end, image_root_visitor, finger_visitor); |
| 290 | } else { |
| 291 | card_table->Scan(mark_bitmap, begin, end, image_root_visitor, IdentityFunctor()); |
| 292 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 296 | class CheckBitmapVisitor { |
| 297 | public: |
| 298 | CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) { |
| 299 | |
| 300 | } |
| 301 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 302 | void operator ()(const Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 303 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, |
| 304 | Locks::mutator_lock_) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 305 | DCHECK(obj != NULL); |
| 306 | mark_sweep_->CheckObject(obj); |
| 307 | } |
| 308 | |
| 309 | private: |
| 310 | MarkSweep* mark_sweep_; |
| 311 | }; |
| 312 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 313 | void MarkSweep::VerifyImageRoots() { |
| 314 | // Verify roots ensures that all the references inside the image space point |
| 315 | // objects which are either in the image space or marked objects in the alloc |
| 316 | // space |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 317 | CheckBitmapVisitor visitor(this); |
| 318 | const Spaces& spaces = heap_->GetSpaces(); |
| 319 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 320 | if ((*it)->IsImageSpace()) { |
| 321 | ImageSpace* space = (*it)->AsImageSpace(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 322 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 323 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 324 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 325 | DCHECK(live_bitmap != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 326 | live_bitmap->VisitMarkedRange(begin, end, visitor, IdentityFunctor()); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 331 | class ScanObjectVisitor { |
| 332 | public: |
| 333 | ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) { |
| 334 | |
| 335 | } |
| 336 | |
| 337 | void operator ()(const Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 338 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 339 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 340 | mark_sweep_->ScanObject(obj); |
| 341 | } |
| 342 | |
| 343 | private: |
| 344 | MarkSweep* const mark_sweep_; |
| 345 | }; |
| 346 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 347 | // Populates the mark stack based on the set of marked objects and |
| 348 | // recursively marks until the mark stack is emptied. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 349 | void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 350 | // RecursiveMark will build the lists of known instances of the Reference classes. |
| 351 | // See DelayReferenceReferent for details. |
| 352 | CHECK(soft_reference_list_ == NULL); |
| 353 | CHECK(weak_reference_list_ == NULL); |
| 354 | CHECK(finalizer_reference_list_ == NULL); |
| 355 | CHECK(phantom_reference_list_ == NULL); |
| 356 | CHECK(cleared_reference_list_ == NULL); |
| 357 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 358 | const Spaces& spaces = heap_->GetSpaces(); |
| 359 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 360 | SetFingerVisitor set_finger_visitor(this); |
| 361 | ScanObjectVisitor scan_visitor(this); |
| 362 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 363 | ContinuousSpace* space = *it; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 364 | if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect || |
| 365 | (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 366 | ) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 367 | current_mark_bitmap_ = space->GetMarkBitmap(); |
| 368 | if (current_mark_bitmap_ == NULL) { |
| 369 | GetHeap()->DumpSpaces(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 370 | LOG(FATAL) << "invalid bitmap"; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 371 | } |
| 372 | // This function does not handle heap end increasing, so we must use the space end. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 373 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 374 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 375 | current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 376 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 377 | } |
| 378 | finger_ = reinterpret_cast<Object*>(~0); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 379 | timings.AddSplit("RecursiveMark"); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 380 | // TODO: tune the frequency of emptying the mark stack |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 381 | ProcessMarkStack(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 382 | timings.AddSplit("ProcessMarkStack"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 385 | void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards, |
| 386 | TimingLogger& timings) { |
| 387 | ScanImageRootVisitor image_root_visitor(this); |
| 388 | SetFingerVisitor finger_visitor(this); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 389 | const size_t card_count = cards.size(); |
| 390 | SpaceBitmap* active_bitmap = NULL; |
| 391 | for (size_t i = 0;i < card_count;) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 392 | Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i])); |
| 393 | uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 394 | uintptr_t end = begin + CardTable::kCardSize; |
| 395 | for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < card_count; ++i) { |
| 396 | end += CardTable::kCardSize; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 397 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 398 | if (active_bitmap == NULL || !active_bitmap->HasAddress(start_obj)) { |
| 399 | active_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 400 | #ifndef NDEBUG |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 401 | if (active_bitmap == NULL) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 402 | GetHeap()->DumpSpaces(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 403 | LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 404 | } |
| 405 | #endif |
| 406 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 407 | active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 408 | } |
| 409 | timings.AddSplit("RecursiveMarkCards"); |
| 410 | ProcessMarkStack(); |
| 411 | timings.AddSplit("ProcessMarkStack"); |
| 412 | } |
| 413 | |
| 414 | bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) { |
| 415 | return |
| 416 | reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) || |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 417 | !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object); |
| 418 | } |
| 419 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 420 | void MarkSweep::RecursiveMarkDirtyObjects(bool update_finger) { |
| 421 | ScanGrayObjects(update_finger); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 422 | ProcessMarkStack(); |
| 423 | } |
| 424 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 425 | void MarkSweep::ReMarkRoots() { |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 426 | Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 429 | void MarkSweep::SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 430 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 431 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 432 | IndirectReferenceTable* table = &vm->weak_globals; |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 433 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 434 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 435 | const Object** entry = *it; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 436 | if (!is_marked(*entry, arg)) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 437 | *entry = kClearedJniWeakGlobal; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 442 | struct ArrayMarkedCheck { |
| 443 | ObjectStack* live_stack; |
| 444 | MarkSweep* mark_sweep; |
| 445 | }; |
| 446 | |
| 447 | // Either marked or not live. |
| 448 | bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) { |
| 449 | ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg); |
| 450 | if (array_check->mark_sweep->IsMarked(object)) { |
| 451 | return true; |
| 452 | } |
| 453 | ObjectStack* live_stack = array_check->live_stack; |
| 454 | return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End(); |
| 455 | } |
| 456 | |
| 457 | void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) { |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 458 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 459 | // The callbacks check |
| 460 | // !is_marked where is_marked is the callback but we want |
| 461 | // !IsMarked && IsLive |
| 462 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 463 | // Or for swapped (IsLive || !IsMarked). |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 464 | |
| 465 | ArrayMarkedCheck visitor; |
| 466 | visitor.live_stack = allocations; |
| 467 | visitor.mark_sweep = this; |
| 468 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor); |
| 469 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor); |
| 470 | SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor); |
| 471 | } |
| 472 | |
| 473 | void MarkSweep::SweepSystemWeaks() { |
| 474 | Runtime* runtime = Runtime::Current(); |
| 475 | // The callbacks check |
| 476 | // !is_marked where is_marked is the callback but we want |
| 477 | // !IsMarked && IsLive |
| 478 | // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive). |
| 479 | // Or for swapped (IsLive || !IsMarked). |
| 480 | runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this); |
| 481 | runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this); |
| 482 | SweepJniWeakGlobals(IsMarkedCallback, this); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 485 | bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) { |
| 486 | reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj); |
| 487 | // We don't actually want to sweep the object, so lets return "marked" |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | void MarkSweep::VerifyIsLive(const Object* obj) { |
| 492 | Heap* heap = GetHeap(); |
| 493 | if (!heap->GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 494 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 495 | if (!large_object_space->GetLiveObjects()->Test(obj)) { |
| 496 | if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) == |
| 497 | heap->allocation_stack_->End()) { |
| 498 | // Object not found! |
| 499 | heap->DumpSpaces(); |
| 500 | LOG(FATAL) << "Found dead object " << obj; |
| 501 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void MarkSweep::VerifySystemWeaks() { |
| 507 | Runtime* runtime = Runtime::Current(); |
| 508 | // Verify system weaks, uses a special IsMarked callback which always returns true. |
| 509 | runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this); |
| 510 | runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this); |
| 511 | |
| 512 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 513 | MutexLock mu(Thread::Current(), vm->weak_globals_lock); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 514 | IndirectReferenceTable* table = &vm->weak_globals; |
| 515 | typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto |
| 516 | for (It it = table->begin(), end = table->end(); it != end; ++it) { |
| 517 | const Object** entry = *it; |
| 518 | VerifyIsLive(*entry); |
| 519 | } |
| 520 | } |
| 521 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 522 | struct SweepCallbackContext { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 523 | MarkSweep* mark_sweep; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 524 | AllocSpace* space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 525 | Thread* self; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 526 | }; |
| 527 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame^] | 528 | class CheckpointMarkThreadRoots : public Thread::CheckpointFunction { |
| 529 | public: |
| 530 | CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) { |
| 531 | |
| 532 | } |
| 533 | |
| 534 | virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS { |
| 535 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 536 | Thread* self = Thread::Current(); |
| 537 | DCHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc); |
| 538 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 539 | thread->VisitRoots(MarkSweep::MarkObjectVisitor, mark_sweep_); |
| 540 | mark_sweep_->GetBarrier().Pass(self); |
| 541 | } |
| 542 | |
| 543 | private: |
| 544 | MarkSweep* mark_sweep_; |
| 545 | }; |
| 546 | |
| 547 | Barrier& MarkSweep::GetBarrier() { |
| 548 | return *gc_barrier_; |
| 549 | } |
| 550 | |
| 551 | void MarkSweep::MarkRootsCheckpoint() { |
| 552 | UniquePtr<CheckpointMarkThreadRoots> check_point(new CheckpointMarkThreadRoots(this)); |
| 553 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 554 | // Increment the count of the barrier. If all of the checkpoints have already been finished then |
| 555 | // will hit 0 and continue. Otherwise we are still waiting for some checkpoints, so the counter |
| 556 | // will go positive and we will unblock when it hits zero. |
| 557 | gc_barrier_->Increment(Thread::Current(), thread_list->RunCheckpoint(check_point.get())); |
| 558 | } |
| 559 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 560 | void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 561 | size_t freed_objects = num_ptrs; |
| 562 | size_t freed_bytes = 0; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 563 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 564 | MarkSweep* mark_sweep = context->mark_sweep; |
| 565 | Heap* heap = mark_sweep->GetHeap(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 566 | AllocSpace* space = context->space; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 567 | Thread* self = context->self; |
| 568 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 569 | // Use a bulk free, that merges consecutive objects before freeing or free per object? |
| 570 | // Documentation suggests better free performance with merging, but this may be at the expensive |
| 571 | // of allocation. |
| 572 | // TODO: investigate performance |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 573 | static const bool kUseFreeList = true; |
| 574 | if (kUseFreeList) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 575 | // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 576 | freed_bytes += space->FreeList(self, num_ptrs, ptrs); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 577 | } else { |
| 578 | for (size_t i = 0; i < num_ptrs; ++i) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame^] | 579 | freed_bytes += space->Free(self, ptrs[i]); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 580 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 581 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 582 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 583 | heap->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 584 | mark_sweep->freed_objects_ += freed_objects; |
| 585 | mark_sweep->freed_bytes_ += freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 588 | void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 589 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 590 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 591 | Heap* heap = context->mark_sweep->GetHeap(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 592 | // We don't free any actual memory to avoid dirtying the shared zygote pages. |
| 593 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 594 | Object* obj = static_cast<Object*>(ptrs[i]); |
| 595 | heap->GetLiveBitmap()->Clear(obj); |
| 596 | heap->GetCardTable()->MarkCard(obj); |
| 597 | } |
| 598 | } |
| 599 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 600 | void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 601 | size_t freed_bytes = 0; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 602 | DlMallocSpace* space = heap_->GetAllocSpace(); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 603 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 604 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 605 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
| 606 | // TODO: Fix when sweeping weaks works properly with mutators unpaused + allocation list. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 607 | SweepSystemWeaksArray(allocations); |
| 608 | logger.AddSplit("SweepSystemWeaks"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 609 | |
| 610 | // Newly allocated objects MUST be in the alloc space and those are the only objects which we are |
| 611 | // going to free. |
| 612 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 613 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 614 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
| 615 | SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 616 | SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 617 | if (swap_bitmaps) { |
| 618 | std::swap(live_bitmap, mark_bitmap); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 619 | std::swap(large_live_objects, large_mark_objects); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 622 | size_t freed_large_objects = 0; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 623 | size_t count = allocations->Size(); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 624 | Object** objects = const_cast<Object**>(allocations->Begin()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 625 | Object** out = objects; |
| 626 | |
| 627 | // Empty the allocation stack. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 628 | Thread* self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 629 | for (size_t i = 0;i < count;++i) { |
| 630 | Object* obj = objects[i]; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 631 | // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack. |
| 632 | if (LIKELY(mark_bitmap->HasAddress(obj))) { |
| 633 | if (!mark_bitmap->Test(obj)) { |
| 634 | // Don't bother un-marking since we clear the mark bitmap anyways. |
| 635 | *(out++) = obj; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 636 | } |
| 637 | } else if (!large_mark_objects->Test(obj)) { |
| 638 | ++freed_large_objects; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 639 | freed_bytes += large_object_space->Free(self, obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | logger.AddSplit("Process allocation stack"); |
| 643 | |
| 644 | size_t freed_objects = out - objects; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 645 | freed_bytes += space->FreeList(self, freed_objects, objects); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 646 | VLOG(heap) << "Freed " << freed_objects << "/" << count |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 647 | << " objects with size " << PrettySize(freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 648 | heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes); |
Mathieu Chartier | 40e978b | 2012-09-07 11:38:36 -0700 | [diff] [blame] | 649 | freed_objects_ += freed_objects; |
| 650 | freed_bytes_ += freed_bytes; |
| 651 | logger.AddSplit("FreeList"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 652 | allocations->Reset(); |
| 653 | logger.AddSplit("Reset stack"); |
| 654 | } |
| 655 | |
| 656 | void MarkSweep::Sweep(bool partial, bool swap_bitmaps) { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 657 | DCHECK(mark_stack_->IsEmpty()); |
| 658 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 659 | // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark |
| 660 | // bitmap, resulting in occasional frees of Weaks which are still in use. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 661 | SweepSystemWeaks(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 662 | |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 663 | const Spaces& spaces = heap_->GetSpaces(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 664 | SweepCallbackContext scc; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 665 | scc.mark_sweep = this; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 666 | scc.self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 667 | // TODO: C++0x auto |
| 668 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 669 | ContinuousSpace* space = *it; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 670 | if ( |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 671 | space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect || |
| 672 | (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 673 | ) { |
| 674 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 675 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 676 | scc.space = space->AsAllocSpace(); |
| 677 | SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 678 | SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 679 | if (swap_bitmaps) { |
| 680 | std::swap(live_bitmap, mark_bitmap); |
| 681 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 682 | if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 683 | // 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] | 684 | SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 685 | &SweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 686 | } else { |
| 687 | // 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] | 688 | SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end, |
| 689 | &ZygoteSweepCallback, reinterpret_cast<void*>(&scc)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 690 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 695 | void MarkSweep::SweepLargeObjects(bool swap_bitmaps) { |
| 696 | // Sweep large objects |
| 697 | LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 698 | SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects(); |
| 699 | SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects(); |
| 700 | if (swap_bitmaps) { |
| 701 | std::swap(large_live_objects, large_mark_objects); |
| 702 | } |
| 703 | SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects(); |
| 704 | // O(n*log(n)) but hopefully there are not too many large objects. |
| 705 | size_t freed_objects = 0; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 706 | size_t freed_bytes = 0; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 707 | // TODO: C++0x |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 708 | Thread* self = Thread::Current(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 709 | for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) { |
| 710 | if (!large_mark_objects->Test(*it)) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 711 | freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it)); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 712 | ++freed_objects; |
| 713 | } |
| 714 | } |
| 715 | freed_objects_ += freed_objects; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 716 | freed_bytes_ += freed_bytes; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 717 | // Large objects don't count towards bytes_allocated. |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 718 | GetHeap()->RecordFree(freed_objects, freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 721 | // Scans instance fields. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 722 | inline void MarkSweep::ScanInstanceFields(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 723 | DCHECK(obj != NULL); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 724 | Class* klass = obj->GetClass(); |
| 725 | DCHECK(klass != NULL); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 726 | ScanFields(obj, klass->GetReferenceInstanceOffsets(), false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | // Scans static storage on a Class. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 730 | inline void MarkSweep::ScanStaticFields(const Class* klass) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 731 | DCHECK(klass != NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 732 | ScanFields(klass, klass->GetReferenceStaticOffsets(), true); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 735 | inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 736 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 737 | // Found a reference offset bitmap. Mark the specified offsets. |
| 738 | while (ref_offsets != 0) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 739 | const size_t right_shift = CLZ(ref_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 740 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
| 741 | const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 742 | MarkObject(ref); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 743 | ref_offsets ^= CLASS_HIGH_BIT >> right_shift; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 744 | } |
| 745 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 746 | // There is no reference offset bitmap. In the non-static case, |
| 747 | // walk up the class inheritance hierarchy and find reference |
| 748 | // offsets the hard way. In the static case, just consider this |
| 749 | // class. |
| 750 | for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 751 | klass != NULL; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 752 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 753 | size_t num_reference_fields = (is_static |
| 754 | ? klass->NumReferenceStaticFields() |
| 755 | : klass->NumReferenceInstanceFields()); |
| 756 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 757 | Field* field = (is_static |
| 758 | ? klass->GetStaticField(i) |
| 759 | : klass->GetInstanceField(i)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 760 | MemberOffset field_offset = field->GetOffset(); |
| 761 | const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 762 | MarkObject(ref); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 768 | 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] | 769 | const Spaces& spaces = heap_->GetSpaces(); |
| 770 | // TODO: C++0x auto |
| 771 | for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) { |
| 772 | if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) { |
| 773 | DCHECK(IsMarked(obj)); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 774 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 775 | bool is_marked = IsMarked(ref); |
| 776 | if (!is_marked) { |
| 777 | LOG(INFO) << **cur; |
| 778 | LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref) |
| 779 | << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj) |
| 780 | << "' (" << reinterpret_cast<const void*>(obj) << ") at offset " |
| 781 | << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 782 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 783 | const Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
| 784 | DCHECK(klass != NULL); |
| 785 | const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields(); |
| 786 | DCHECK(fields != NULL); |
| 787 | bool found = false; |
| 788 | for (int32_t i = 0; i < fields->GetLength(); ++i) { |
| 789 | const Field* cur = fields->Get(i); |
| 790 | if (cur->GetOffset().Int32Value() == offset.Int32Value()) { |
| 791 | LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur); |
| 792 | found = true; |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | if (!found) { |
| 797 | LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value(); |
| 798 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 799 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 800 | bool obj_marked = heap_->GetCardTable()->IsDirty(obj); |
| 801 | if (!obj_marked) { |
| 802 | LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' " |
| 803 | << "(" << reinterpret_cast<const void*>(obj) << ") contains references to " |
| 804 | << "the alloc space, but wasn't card marked"; |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 807 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 808 | break; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 812 | // Scans the header, static field references, and interface pointers |
| 813 | // of a class object. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 814 | inline void MarkSweep::ScanClass(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 815 | #ifndef NDEBUG |
| 816 | ++class_count_; |
| 817 | #endif |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 818 | ScanInstanceFields(obj); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 819 | ScanStaticFields(obj->AsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | // Scans the header of all array objects. If the array object is |
| 823 | // specialized to a reference type, scans the array data as well. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 824 | inline void MarkSweep::ScanArray(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 825 | #ifndef NDEBUG |
| 826 | ++array_count_; |
| 827 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 828 | MarkObject(obj->GetClass()); |
| 829 | if (obj->IsObjectArray()) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 830 | const ObjectArray<Object>* array = obj->AsObjectArray<Object>(); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 831 | for (int32_t i = 0; i < array->GetLength(); ++i) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 832 | const Object* element = array->GetWithoutChecks(i); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 833 | MarkObject(element); |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 838 | // Process the "referent" field in a java.lang.ref.Reference. If the |
| 839 | // referent has not yet been marked, put it on the appropriate list in |
| 840 | // the gcHeap for later processing. |
| 841 | void MarkSweep::DelayReferenceReferent(Object* obj) { |
| 842 | DCHECK(obj != NULL); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 843 | Class* klass = obj->GetClass(); |
| 844 | DCHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 845 | DCHECK(klass->IsReferenceClass()); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 846 | Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false); |
| 847 | Object* referent = heap_->GetReferenceReferent(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 848 | if (pending == NULL && referent != NULL && !IsMarked(referent)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 849 | Object** list = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 850 | if (klass->IsSoftReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 851 | list = &soft_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 852 | } else if (klass->IsWeakReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 853 | list = &weak_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 854 | } else if (klass->IsFinalizerReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 855 | list = &finalizer_reference_list_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 856 | } else if (klass->IsPhantomReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 857 | list = &phantom_reference_list_; |
| 858 | } |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 859 | DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 860 | heap_->EnqueuePendingReference(obj, list); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
| 864 | // Scans the header and field references of a data object. If the |
| 865 | // scanned object is a reference subclass, it is scheduled for later |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 866 | // processing. |
Elliott Hughes | b066311 | 2011-10-19 18:16:37 -0700 | [diff] [blame] | 867 | inline void MarkSweep::ScanOther(const Object* obj) { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 868 | #ifndef NDEBUG |
| 869 | ++other_count_; |
| 870 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 871 | ScanInstanceFields(obj); |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 872 | if (obj->GetClass()->IsReferenceClass()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 873 | DelayReferenceReferent(const_cast<Object*>(obj)); |
| 874 | } |
| 875 | } |
| 876 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 877 | void MarkSweep::ScanRoot(const Object* obj) { |
| 878 | ScanObject(obj); |
| 879 | } |
| 880 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 881 | // Scans an object reference. Determines the type of the reference |
| 882 | // and dispatches to a specialized scanning routine. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 883 | void MarkSweep::ScanObject(const Object* obj) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 884 | DCHECK(obj != NULL); |
| 885 | DCHECK(obj->GetClass() != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 886 | #ifndef NDEBUG |
| 887 | if (!IsMarked(obj)) { |
| 888 | heap_->DumpSpaces(); |
| 889 | LOG(FATAL) << "Scanning unmarked object " << reinterpret_cast<const void*>(obj); |
| 890 | } |
| 891 | #endif |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 892 | if (obj->IsClass()) { |
| 893 | ScanClass(obj); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 894 | } else if (obj->IsArrayInstance()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 895 | ScanArray(obj); |
| 896 | } else { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 897 | ScanOther(obj); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 901 | // Scan anything that's on the mark stack. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 902 | void MarkSweep::ProcessMarkStack() { |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 903 | if (kUseMarkStackPrefetch) { |
| 904 | const size_t fifo_size = 4; |
| 905 | const size_t fifo_mask = fifo_size - 1; |
| 906 | const Object* fifo[fifo_size]; |
| 907 | for (size_t i = 0;i < fifo_size;++i) { |
| 908 | fifo[i] = NULL; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 909 | } |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 910 | size_t fifo_pos = 0; |
| 911 | size_t fifo_count = 0; |
| 912 | for (;;) { |
| 913 | const Object* obj = fifo[fifo_pos & fifo_mask]; |
| 914 | if (obj != NULL) { |
| 915 | ScanObject(obj); |
| 916 | fifo[fifo_pos & fifo_mask] = NULL; |
| 917 | --fifo_count; |
| 918 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 919 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 920 | if (!mark_stack_->IsEmpty()) { |
| 921 | const Object* obj = mark_stack_->PopBack(); |
| 922 | DCHECK(obj != NULL); |
| 923 | fifo[fifo_pos & fifo_mask] = obj; |
| 924 | __builtin_prefetch(obj); |
| 925 | fifo_count++; |
| 926 | } |
| 927 | fifo_pos++; |
| 928 | |
| 929 | if (!fifo_count) { |
| 930 | CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size(); |
| 931 | break; |
| 932 | } |
| 933 | } |
| 934 | } else { |
| 935 | while (!mark_stack_->IsEmpty()) { |
| 936 | const Object* obj = mark_stack_->PopBack(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 937 | DCHECK(obj != NULL); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 938 | ScanObject(obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 943 | // Walks the reference list marking any references subject to the |
| 944 | // reference clearing policy. References with a black referent are |
| 945 | // removed from the list. References with white referents biased |
| 946 | // toward saving are blackened and also removed from the list. |
| 947 | void MarkSweep::PreserveSomeSoftReferences(Object** list) { |
| 948 | DCHECK(list != NULL); |
| 949 | Object* clear = NULL; |
| 950 | size_t counter = 0; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 951 | |
| 952 | DCHECK(mark_stack_->IsEmpty()); |
| 953 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 954 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 955 | Object* ref = heap_->DequeuePendingReference(list); |
| 956 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 957 | if (referent == NULL) { |
| 958 | // Referent was cleared by the user during marking. |
| 959 | continue; |
| 960 | } |
| 961 | bool is_marked = IsMarked(referent); |
| 962 | if (!is_marked && ((++counter) & 1)) { |
| 963 | // Referent is white and biased toward saving, mark it. |
| 964 | MarkObject(referent); |
| 965 | is_marked = true; |
| 966 | } |
| 967 | if (!is_marked) { |
| 968 | // Referent is white, queue it for clearing. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 969 | heap_->EnqueuePendingReference(ref, &clear); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | *list = clear; |
| 973 | // Restart the mark with the newly black references added to the |
| 974 | // root set. |
| 975 | ProcessMarkStack(); |
| 976 | } |
| 977 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 978 | inline bool MarkSweep::IsMarked(const Object* object) const |
| 979 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 980 | if (object >= immune_begin_ && object < immune_end_) { |
| 981 | return true; |
| 982 | } |
| 983 | DCHECK(current_mark_bitmap_ != NULL); |
| 984 | if (current_mark_bitmap_->HasAddress(object)) { |
| 985 | return current_mark_bitmap_->Test(object); |
| 986 | } |
| 987 | return heap_->GetMarkBitmap()->Test(object); |
| 988 | } |
| 989 | |
| 990 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 991 | // Unlink the reference list clearing references objects with white |
| 992 | // referents. Cleared references registered to a reference queue are |
| 993 | // scheduled for appending by the heap worker thread. |
| 994 | void MarkSweep::ClearWhiteReferences(Object** list) { |
| 995 | DCHECK(list != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 996 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 997 | Object* ref = heap_->DequeuePendingReference(list); |
| 998 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 999 | if (referent != NULL && !IsMarked(referent)) { |
| 1000 | // Referent is white, clear it. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1001 | heap_->ClearReferenceReferent(ref); |
| 1002 | if (heap_->IsEnqueuable(ref)) { |
| 1003 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | DCHECK(*list == NULL); |
| 1008 | } |
| 1009 | |
| 1010 | // Enqueues finalizer references with white referents. White |
| 1011 | // referents are blackened, moved to the zombie field, and the |
| 1012 | // referent field is cleared. |
| 1013 | void MarkSweep::EnqueueFinalizerReferences(Object** list) { |
| 1014 | DCHECK(list != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1015 | MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1016 | bool has_enqueued = false; |
| 1017 | while (*list != NULL) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1018 | Object* ref = heap_->DequeuePendingReference(list); |
| 1019 | Object* referent = heap_->GetReferenceReferent(ref); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1020 | if (referent != NULL && !IsMarked(referent)) { |
| 1021 | MarkObject(referent); |
| 1022 | // If the referent is non-null the reference must queuable. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1023 | DCHECK(heap_->IsEnqueuable(ref)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1024 | ref->SetFieldObject(zombie_offset, referent, false); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1025 | heap_->ClearReferenceReferent(ref); |
| 1026 | heap_->EnqueueReference(ref, &cleared_reference_list_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1027 | has_enqueued = true; |
| 1028 | } |
| 1029 | } |
| 1030 | if (has_enqueued) { |
| 1031 | ProcessMarkStack(); |
| 1032 | } |
| 1033 | DCHECK(*list == NULL); |
| 1034 | } |
| 1035 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1036 | // Process reference class instances and schedule finalizations. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1037 | void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft, |
| 1038 | Object** weak_references, |
| 1039 | Object** finalizer_references, |
| 1040 | Object** phantom_references) { |
| 1041 | DCHECK(soft_references != NULL); |
| 1042 | DCHECK(weak_references != NULL); |
| 1043 | DCHECK(finalizer_references != NULL); |
| 1044 | DCHECK(phantom_references != NULL); |
| 1045 | |
| 1046 | // Unless we are in the zygote or required to clear soft references |
| 1047 | // with white references, preserve some white referents. |
Ian Rogers | 2945e24 | 2012-06-03 14:45:16 -0700 | [diff] [blame] | 1048 | if (!clear_soft && !Runtime::Current()->IsZygote()) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1049 | PreserveSomeSoftReferences(soft_references); |
| 1050 | } |
| 1051 | |
| 1052 | // Clear all remaining soft and weak references with white |
| 1053 | // referents. |
| 1054 | ClearWhiteReferences(soft_references); |
| 1055 | ClearWhiteReferences(weak_references); |
| 1056 | |
| 1057 | // Preserve all white objects with finalize methods and schedule |
| 1058 | // them for finalization. |
| 1059 | EnqueueFinalizerReferences(finalizer_references); |
| 1060 | |
| 1061 | // Clear all f-reachable soft and weak references with white |
| 1062 | // referents. |
| 1063 | ClearWhiteReferences(soft_references); |
| 1064 | ClearWhiteReferences(weak_references); |
| 1065 | |
| 1066 | // Clear all phantom references with white referents. |
| 1067 | ClearWhiteReferences(phantom_references); |
| 1068 | |
| 1069 | // At this point all reference lists should be empty. |
| 1070 | DCHECK(*soft_references == NULL); |
| 1071 | DCHECK(*weak_references == NULL); |
| 1072 | DCHECK(*finalizer_references == NULL); |
| 1073 | DCHECK(*phantom_references == NULL); |
| 1074 | } |
| 1075 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1076 | void MarkSweep::UnBindBitmaps() { |
| 1077 | const Spaces& spaces = heap_->GetSpaces(); |
| 1078 | // TODO: C++0x auto |
| 1079 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
| 1080 | Space* space = *it; |
| 1081 | if (space->IsAllocSpace()) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 1082 | DlMallocSpace* alloc_space = space->AsAllocSpace(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1083 | if (alloc_space->temp_bitmap_.get() != NULL) { |
| 1084 | // At this point, the temp_bitmap holds our old mark bitmap. |
| 1085 | SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release(); |
| 1086 | GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap); |
| 1087 | CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get()); |
| 1088 | alloc_space->mark_bitmap_.reset(new_bitmap); |
| 1089 | DCHECK(alloc_space->temp_bitmap_.get() == NULL); |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1095 | MarkSweep::~MarkSweep() { |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 1096 | #ifndef NDEBUG |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1097 | VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_; |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 1098 | #endif |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1099 | // Ensure that the mark stack is empty. |
| 1100 | CHECK(mark_stack_->IsEmpty()); |
| 1101 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1102 | // Clear all of the alloc spaces' mark bitmaps. |
| 1103 | const Spaces& spaces = heap_->GetSpaces(); |
| 1104 | // TODO: C++0x auto |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1105 | for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1106 | ContinuousSpace* space = *it; |
| 1107 | if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) { |
| 1108 | space->GetMarkBitmap()->Clear(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 1111 | mark_stack_->Reset(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1112 | |
| 1113 | // Reset the marked large objects. |
| 1114 | LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1115 | large_objects->GetMarkObjects()->Clear(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | } // namespace art |