blob: e93eb1af980ddaf21cd959a4c12ecc36480b8603 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "mark_sweep.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Carl Shapiro58551df2011-07-24 03:09:51 -070019#include <climits>
20#include <vector>
21
Mathieu Chartier858f1c52012-10-17 17:45:55 -070022#include "barrier.h"
Mathieu Chartier357e9be2012-08-01 11:00:14 -070023#include "card_table.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070024#include "class_loader.h"
Brian Carlstrom693267a2011-09-06 09:25:34 -070025#include "dex_cache.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070026#include "heap.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070027#include "indirect_reference_table.h"
28#include "intern_table.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070029#include "jni_internal.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070030#include "large_object_space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "logging.h"
32#include "macros.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070033#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070035#include "runtime.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070036#include "space.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070037#include "timing_logger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070038#include "thread.h"
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070039#include "thread_list.h"
Ian Rogers08254272012-10-23 17:49:23 -070040#include "verifier/method_verifier.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070041
Carl Shapiro69759ea2011-07-21 18:13:35 -070042namespace art {
43
Mathieu Chartier858f1c52012-10-17 17:45:55 -070044static const bool kUseMarkStackPrefetch = true;
45
Mathieu Chartier357e9be2012-08-01 11:00:14 -070046class SetFingerVisitor {
47 public:
48 SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
49
50 }
51
52 void operator ()(void* finger) const {
53 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
54 }
55
56 private:
57 MarkSweep* const mark_sweep_;
58};
59
Mathieu Chartierd8195f12012-10-05 12:21:28 -070060MarkSweep::MarkSweep(ObjectStack* mark_stack)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070061 : current_mark_bitmap_(NULL),
62 mark_stack_(mark_stack),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070063 heap_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070064 finger_(NULL),
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070065 immune_begin_(NULL),
66 immune_end_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070067 soft_reference_list_(NULL),
68 weak_reference_list_(NULL),
69 finalizer_reference_list_(NULL),
70 phantom_reference_list_(NULL),
71 cleared_reference_list_(NULL),
Mathieu Chartier357e9be2012-08-01 11:00:14 -070072 freed_bytes_(0), freed_objects_(0),
Mathieu Chartier858f1c52012-10-17 17:45:55 -070073 class_count_(0), array_count_(0), other_count_(0),
74 gc_barrier_(new Barrier) {
Mathieu Chartier5301cd22012-05-31 12:11:36 -070075 DCHECK(mark_stack_ != NULL);
76}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080077
Mathieu Chartier5301cd22012-05-31 12:11:36 -070078void MarkSweep::Init() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080079 heap_ = Runtime::Current()->GetHeap();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070080 mark_stack_->Reset();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070081 // TODO: C++0x auto
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070082 FindDefaultMarkBitmap();
buzbee0d966cf2011-09-08 17:34:58 -070083 // TODO: if concurrent, enable card marking in compiler
Carl Shapiro58551df2011-07-24 03:09:51 -070084 // TODO: check that the mark bitmap is entirely clear.
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -070085 // Mark any concurrent roots as dirty since we need to scan them at least once during this GC.
86 Runtime::Current()->DirtyRoots();
Carl Shapiro58551df2011-07-24 03:09:51 -070087}
88
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070089void MarkSweep::FindDefaultMarkBitmap() {
90 const Spaces& spaces = heap_->GetSpaces();
91 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
92 if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
93 current_mark_bitmap_ = (*it)->GetMarkBitmap();
94 CHECK(current_mark_bitmap_ != NULL);
95 return;
96 }
97 }
98 GetHeap()->DumpSpaces();
99 LOG(FATAL) << "Could not find a default mark bitmap";
100}
101
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700102inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700103 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700104
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700105 if (obj >= immune_begin_ && obj < immune_end_) {
106 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700107 return;
108 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700109
110 // Try to take advantage of locality of references within a space, failing this find the space
111 // the hard way.
Ian Rogers506de0c2012-09-17 15:39:06 -0700112 if (UNLIKELY(!current_mark_bitmap_->HasAddress(obj))) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700113 SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
114 if (new_bitmap != NULL) {
115 current_mark_bitmap_ = new_bitmap;
116 } else {
117 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
118 SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
119 if (!large_objects->Test(obj)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700120 if (!large_object_space->Contains(obj)) {
121 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
122 LOG(ERROR) << "Attempting see if it's a bad root";
123 VerifyRoots();
124 LOG(FATAL) << "Can't mark bad root";
125 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700126 large_objects->Set(obj);
127 // Don't need to check finger since large objects never have any object references.
128 }
129 // TODO: Improve clarity of control flow in this function?
130 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700131 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700132 }
133
Carl Shapiro69759ea2011-07-21 18:13:35 -0700134 // This object was not previously marked.
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700135 if (!current_mark_bitmap_->Test(obj)) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700136 current_mark_bitmap_->Set(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700137 if (check_finger && obj < finger_) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700138 // Do we need to expand the mark stack?
139 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
140 std::vector<Object*> temp;
141 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
142 mark_stack_->Resize(mark_stack_->Capacity() * 2);
143 for (size_t i = 0; i < temp.size(); ++i) {
144 mark_stack_->PushBack(temp[i]);
145 }
146 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700147 // The object must be pushed on to the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700148 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700149 }
150 }
151}
152
153// Used to mark objects when recursing. Recursion is done by moving
154// the finger across the bitmaps in address order and marking child
155// objects. Any newly-marked objects whose addresses are lower than
156// the finger won't be visited by the bitmap scan, so those objects
157// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700158void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700159 if (obj != NULL) {
160 MarkObject0(obj, true);
161 }
162}
163
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700164void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700165 DCHECK(root != NULL);
166 DCHECK(arg != NULL);
167 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700168 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700169}
170
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700171void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
172 DCHECK(root != NULL);
173 DCHECK(arg != NULL);
174 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
175 mark_sweep->MarkObject0(root, true);
176}
177
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700178void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
179 const AbstractMethod* method) {
180 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, method);
181}
182
183void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const AbstractMethod* method) {
184 // See if the root is on any space bitmap.
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700185 if (GetHeap()->GetLiveBitmap()->GetSpaceBitmap(root) == NULL) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700186 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier4202b742012-10-17 17:51:25 -0700187 if (!large_object_space->Contains(root)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700188 LOG(ERROR) << "Found invalid root: " << root;
Ian Rogers08254272012-10-23 17:49:23 -0700189 LOG(ERROR) << "VReg: " << vreg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700190 if (method != NULL) {
Ian Rogers08254272012-10-23 17:49:23 -0700191 LOG(ERROR) << "In method " << PrettyMethod(method, true) << "\nVerifier output:\n";
192 verifier::MethodVerifier::VerifyMethodAndDump(const_cast<AbstractMethod*>(method));
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700193 }
194 }
195 }
196}
197
198void MarkSweep::VerifyRoots() {
199 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
200}
201
Carl Shapiro69759ea2011-07-21 18:13:35 -0700202// Marks all objects in the root set.
203void MarkSweep::MarkRoots() {
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700204 Runtime::Current()->VisitNonConcurrentRoots(MarkObjectVisitor, this);
205}
206
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700207void MarkSweep::MarkNonThreadRoots() {
208 Runtime::Current()->VisitNonThreadRoots(MarkObjectVisitor, this);
209}
210
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700211void MarkSweep::MarkConcurrentRoots() {
212 Runtime::Current()->VisitConcurrentRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700213}
214
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700215class CheckObjectVisitor {
216 public:
217 CheckObjectVisitor(MarkSweep* const mark_sweep)
218 : mark_sweep_(mark_sweep) {
219
220 }
221
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
224 Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700225 mark_sweep_->CheckReference(obj, ref, offset, is_static);
226 }
227
228 private:
229 MarkSweep* const mark_sweep_;
230};
231
232void MarkSweep::CheckObject(const Object* obj) {
233 DCHECK(obj != NULL);
234 CheckObjectVisitor visitor(this);
235 VisitObjectReferences(obj, visitor);
236}
237
238void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
239 DCHECK(root != NULL);
240 DCHECK(arg != NULL);
241 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700242 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700243 mark_sweep->CheckObject(root);
244}
245
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700246void MarkSweep::CopyMarkBits(ContinuousSpace* space) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700247 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
248 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
249 mark_bitmap->CopyFrom(live_bitmap);
Ian Rogers5d76c432011-10-31 21:42:49 -0700250}
251
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700252void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) {
253 CHECK(space->IsAllocSpace());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700254 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700255 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
256 SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
257 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
258 alloc_space->temp_bitmap_.reset(mark_bitmap);
259 alloc_space->mark_bitmap_.reset(live_bitmap);
260}
261
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700262class ScanImageRootVisitor {
263 public:
264 ScanImageRootVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700265 }
266
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 void operator ()(const Object* root) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
269 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700270 DCHECK(root != NULL);
271 mark_sweep_->ScanObject(root);
272 }
273
274 private:
275 MarkSweep* const mark_sweep_;
276};
277
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700278void MarkSweep::ScanGrayObjects(bool update_finger) {
279 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700280 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700281 ScanImageRootVisitor image_root_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700282 SetFingerVisitor finger_visitor(this);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700283 // TODO: C++ 0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700284 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700285 ContinuousSpace* space = *it;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700286 byte* begin = space->Begin();
287 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700288 // Image spaces are handled properly since live == marked for them.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700289 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
290 if (update_finger) {
291 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, finger_visitor);
292 } else {
293 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, IdentityFunctor());
294 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700295 }
296}
297
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700298class CheckBitmapVisitor {
299 public:
300 CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
301
302 }
303
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700305 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
306 Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700307 DCHECK(obj != NULL);
308 mark_sweep_->CheckObject(obj);
309 }
310
311 private:
312 MarkSweep* mark_sweep_;
313};
314
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700315void MarkSweep::VerifyImageRoots() {
316 // Verify roots ensures that all the references inside the image space point
317 // objects which are either in the image space or marked objects in the alloc
318 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700319 CheckBitmapVisitor visitor(this);
320 const Spaces& spaces = heap_->GetSpaces();
321 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700322 if ((*it)->IsImageSpace()) {
323 ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700324 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
325 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
326 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700327 DCHECK(live_bitmap != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700328 live_bitmap->VisitMarkedRange(begin, end, visitor, IdentityFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700329 }
330 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700331}
332
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700333class ScanObjectVisitor {
334 public:
335 ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
336
337 }
338
339 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700340 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700342 mark_sweep_->ScanObject(obj);
343 }
344
345 private:
346 MarkSweep* const mark_sweep_;
347};
348
Carl Shapiro58551df2011-07-24 03:09:51 -0700349// Populates the mark stack based on the set of marked objects and
350// recursively marks until the mark stack is emptied.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700351void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700352 // RecursiveMark will build the lists of known instances of the Reference classes.
353 // See DelayReferenceReferent for details.
354 CHECK(soft_reference_list_ == NULL);
355 CHECK(weak_reference_list_ == NULL);
356 CHECK(finalizer_reference_list_ == NULL);
357 CHECK(phantom_reference_list_ == NULL);
358 CHECK(cleared_reference_list_ == NULL);
359
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700360 const Spaces& spaces = heap_->GetSpaces();
361
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700362 SetFingerVisitor set_finger_visitor(this);
363 ScanObjectVisitor scan_visitor(this);
364 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700365 ContinuousSpace* space = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700366 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
367 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700368 ) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700369 current_mark_bitmap_ = space->GetMarkBitmap();
370 if (current_mark_bitmap_ == NULL) {
371 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700372 LOG(FATAL) << "invalid bitmap";
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700373 }
374 // This function does not handle heap end increasing, so we must use the space end.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700375 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
376 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700377 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700378 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700379 }
380 finger_ = reinterpret_cast<Object*>(~0);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700381 timings.AddSplit("RecursiveMark");
Ian Rogers5d76c432011-10-31 21:42:49 -0700382 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700383 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700384 timings.AddSplit("ProcessMarkStack");
Carl Shapiro58551df2011-07-24 03:09:51 -0700385}
386
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700387void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
388 TimingLogger& timings) {
389 ScanImageRootVisitor image_root_visitor(this);
390 SetFingerVisitor finger_visitor(this);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700391 const size_t card_count = cards.size();
392 SpaceBitmap* active_bitmap = NULL;
393 for (size_t i = 0;i < card_count;) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700394 Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i]));
395 uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700396 uintptr_t end = begin + CardTable::kCardSize;
397 for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < card_count; ++i) {
398 end += CardTable::kCardSize;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700399 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700400 if (active_bitmap == NULL || !active_bitmap->HasAddress(start_obj)) {
401 active_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700402#ifndef NDEBUG
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700403 if (active_bitmap == NULL) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700404 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700405 LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700406 }
407#endif
408 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700409 active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700410 }
411 timings.AddSplit("RecursiveMarkCards");
412 ProcessMarkStack();
413 timings.AddSplit("ProcessMarkStack");
414}
415
416bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
417 return
418 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700419 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
420}
421
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700422void MarkSweep::RecursiveMarkDirtyObjects(bool update_finger) {
423 ScanGrayObjects(update_finger);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700424 ProcessMarkStack();
425}
426
Carl Shapiro58551df2011-07-24 03:09:51 -0700427void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700428 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700429}
430
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700431void MarkSweep::SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700432 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700433 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700434 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700435 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700436 for (It it = table->begin(), end = table->end(); it != end; ++it) {
437 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700438 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700439 *entry = kClearedJniWeakGlobal;
440 }
441 }
442}
443
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700444struct ArrayMarkedCheck {
445 ObjectStack* live_stack;
446 MarkSweep* mark_sweep;
447};
448
449// Either marked or not live.
450bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
451 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
452 if (array_check->mark_sweep->IsMarked(object)) {
453 return true;
454 }
455 ObjectStack* live_stack = array_check->live_stack;
456 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
457}
458
459void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700460 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700461 // The callbacks check
462 // !is_marked where is_marked is the callback but we want
463 // !IsMarked && IsLive
464 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
465 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700466
467 ArrayMarkedCheck visitor;
468 visitor.live_stack = allocations;
469 visitor.mark_sweep = this;
470 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
471 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
472 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
473}
474
475void MarkSweep::SweepSystemWeaks() {
476 Runtime* runtime = Runtime::Current();
477 // The callbacks check
478 // !is_marked where is_marked is the callback but we want
479 // !IsMarked && IsLive
480 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
481 // Or for swapped (IsLive || !IsMarked).
482 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
483 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
484 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700485}
486
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700487bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
488 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
489 // We don't actually want to sweep the object, so lets return "marked"
490 return true;
491}
492
493void MarkSweep::VerifyIsLive(const Object* obj) {
494 Heap* heap = GetHeap();
495 if (!heap->GetLiveBitmap()->Test(obj)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700496 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
497 if (!large_object_space->GetLiveObjects()->Test(obj)) {
498 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
499 heap->allocation_stack_->End()) {
500 // Object not found!
501 heap->DumpSpaces();
502 LOG(FATAL) << "Found dead object " << obj;
503 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700504 }
505 }
506}
507
508void MarkSweep::VerifySystemWeaks() {
509 Runtime* runtime = Runtime::Current();
510 // Verify system weaks, uses a special IsMarked callback which always returns true.
511 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
512 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
513
514 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700515 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700516 IndirectReferenceTable* table = &vm->weak_globals;
517 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
518 for (It it = table->begin(), end = table->end(); it != end; ++it) {
519 const Object** entry = *it;
520 VerifyIsLive(*entry);
521 }
522}
523
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800524struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700525 MarkSweep* mark_sweep;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800526 AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700527 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800528};
529
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700530class CheckpointMarkThreadRoots : public Closure {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700531 public:
532 CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
533
534 }
535
536 virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
537 // Note: self is not necessarily equal to thread since thread may be suspended.
538 Thread* self = Thread::Current();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700539 DCHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc)
540 << thread->GetState();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700541 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
542 thread->VisitRoots(MarkSweep::MarkObjectVisitor, mark_sweep_);
543 mark_sweep_->GetBarrier().Pass(self);
544 }
545
546 private:
547 MarkSweep* mark_sweep_;
548};
549
550Barrier& MarkSweep::GetBarrier() {
551 return *gc_barrier_;
552}
553
554void MarkSweep::MarkRootsCheckpoint() {
555 UniquePtr<CheckpointMarkThreadRoots> check_point(new CheckpointMarkThreadRoots(this));
556 ThreadList* thread_list = Runtime::Current()->GetThreadList();
557 // Increment the count of the barrier. If all of the checkpoints have already been finished then
558 // will hit 0 and continue. Otherwise we are still waiting for some checkpoints, so the counter
559 // will go positive and we will unblock when it hits zero.
560 gc_barrier_->Increment(Thread::Current(), thread_list->RunCheckpoint(check_point.get()));
561}
562
Ian Rogers30fab402012-01-23 15:43:46 -0800563void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700564 size_t freed_objects = num_ptrs;
565 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800566 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700567 MarkSweep* mark_sweep = context->mark_sweep;
568 Heap* heap = mark_sweep->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800569 AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700570 Thread* self = context->self;
571 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700572 // Use a bulk free, that merges consecutive objects before freeing or free per object?
573 // Documentation suggests better free performance with merging, but this may be at the expensive
574 // of allocation.
575 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800576 static const bool kUseFreeList = true;
577 if (kUseFreeList) {
Ian Rogers30fab402012-01-23 15:43:46 -0800578 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700579 freed_bytes += space->FreeList(self, num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700580 } else {
581 for (size_t i = 0; i < num_ptrs; ++i) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700582 freed_bytes += space->Free(self, ptrs[i]);
Ian Rogers5d76c432011-10-31 21:42:49 -0700583 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700584 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700585
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700586 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700587 mark_sweep->freed_objects_ += freed_objects;
588 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700589}
590
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700591void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700592 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700593 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700594 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700595 // We don't free any actual memory to avoid dirtying the shared zygote pages.
596 for (size_t i = 0; i < num_ptrs; ++i) {
597 Object* obj = static_cast<Object*>(ptrs[i]);
598 heap->GetLiveBitmap()->Clear(obj);
599 heap->GetCardTable()->MarkCard(obj);
600 }
601}
602
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700603void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700604 size_t freed_bytes = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700605 DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700606
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700607 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
608 // bitmap, resulting in occasional frees of Weaks which are still in use.
609 // TODO: Fix when sweeping weaks works properly with mutators unpaused + allocation list.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700610 SweepSystemWeaksArray(allocations);
611 logger.AddSplit("SweepSystemWeaks");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700612
613 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
614 // going to free.
615 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
616 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700617 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
618 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
619 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700620 if (swap_bitmaps) {
621 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700622 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700623 }
624
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700625 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700626 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700627 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700628 Object** out = objects;
629
630 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700631 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700632 for (size_t i = 0;i < count;++i) {
633 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700634 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
635 if (LIKELY(mark_bitmap->HasAddress(obj))) {
636 if (!mark_bitmap->Test(obj)) {
637 // Don't bother un-marking since we clear the mark bitmap anyways.
638 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700639 }
640 } else if (!large_mark_objects->Test(obj)) {
641 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700642 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700643 }
644 }
645 logger.AddSplit("Process allocation stack");
646
647 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700648 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700649 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700650 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700651 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700652 freed_objects_ += freed_objects;
653 freed_bytes_ += freed_bytes;
654 logger.AddSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700655 allocations->Reset();
656 logger.AddSplit("Reset stack");
657}
658
659void MarkSweep::Sweep(bool partial, bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700660 DCHECK(mark_stack_->IsEmpty());
661
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700662 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
663 // bitmap, resulting in occasional frees of Weaks which are still in use.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700664 SweepSystemWeaks();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700665
Mathieu Chartier46a23632012-08-07 18:44:40 -0700666 const Spaces& spaces = heap_->GetSpaces();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800667 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700668 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700669 scc.self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700670 // TODO: C++0x auto
671 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700672 ContinuousSpace* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700673 if (
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700674 space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
675 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700676 ) {
677 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
678 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
679 scc.space = space->AsAllocSpace();
680 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
681 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700682 if (swap_bitmaps) {
683 std::swap(live_bitmap, mark_bitmap);
684 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700685 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700686 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700687 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
688 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700689 } else {
690 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700691 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
692 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700693 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700694 }
695 }
696}
697
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700698void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
699 // Sweep large objects
700 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700701 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
702 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
703 if (swap_bitmaps) {
704 std::swap(large_live_objects, large_mark_objects);
705 }
706 SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
707 // O(n*log(n)) but hopefully there are not too many large objects.
708 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700709 size_t freed_bytes = 0;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700710 // TODO: C++0x
Ian Rogers50b35e22012-10-04 10:09:15 -0700711 Thread* self = Thread::Current();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700712 for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) {
713 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700714 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700715 ++freed_objects;
716 }
717 }
718 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700719 freed_bytes_ += freed_bytes;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700720 // Large objects don't count towards bytes_allocated.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700721 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700722}
723
Carl Shapiro69759ea2011-07-21 18:13:35 -0700724// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700725inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700726 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700727 Class* klass = obj->GetClass();
728 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700729 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700730}
731
732// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700733inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700734 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700735 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700736}
737
Elliott Hughesb0663112011-10-19 18:16:37 -0700738inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700739 if (ref_offsets != CLASS_WALK_SUPER) {
740 // Found a reference offset bitmap. Mark the specified offsets.
741 while (ref_offsets != 0) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700742 const size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700743 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
744 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700745 MarkObject(ref);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700746 ref_offsets ^= CLASS_HIGH_BIT >> right_shift;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700747 }
748 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700749 // There is no reference offset bitmap. In the non-static case,
750 // walk up the class inheritance hierarchy and find reference
751 // offsets the hard way. In the static case, just consider this
752 // class.
753 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700754 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700755 klass = is_static ? NULL : klass->GetSuperClass()) {
756 size_t num_reference_fields = (is_static
757 ? klass->NumReferenceStaticFields()
758 : klass->NumReferenceInstanceFields());
759 for (size_t i = 0; i < num_reference_fields; ++i) {
760 Field* field = (is_static
761 ? klass->GetStaticField(i)
762 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700763 MemberOffset field_offset = field->GetOffset();
764 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700765 MarkObject(ref);
766 }
767 }
768 }
769}
770
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700771void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700772 const Spaces& spaces = heap_->GetSpaces();
773 // TODO: C++0x auto
774 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
775 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
776 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700777
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700778 bool is_marked = IsMarked(ref);
779 if (!is_marked) {
780 LOG(INFO) << **cur;
781 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
782 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
783 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
784 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700785
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700786 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
787 DCHECK(klass != NULL);
788 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
789 DCHECK(fields != NULL);
790 bool found = false;
791 for (int32_t i = 0; i < fields->GetLength(); ++i) {
792 const Field* cur = fields->Get(i);
793 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
794 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
795 found = true;
796 break;
797 }
798 }
799 if (!found) {
800 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
801 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700802
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700803 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
804 if (!obj_marked) {
805 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
806 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
807 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700808 }
809 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700810 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700811 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700812 }
813}
814
Carl Shapiro69759ea2011-07-21 18:13:35 -0700815// Scans the header, static field references, and interface pointers
816// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700817inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700818#ifndef NDEBUG
819 ++class_count_;
820#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700821 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700822 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700823}
824
825// Scans the header of all array objects. If the array object is
826// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700827inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700828#ifndef NDEBUG
829 ++array_count_;
830#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700831 MarkObject(obj->GetClass());
832 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700833 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700834 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700835 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700836 MarkObject(element);
837 }
838 }
839}
840
Carl Shapiro69759ea2011-07-21 18:13:35 -0700841// Process the "referent" field in a java.lang.ref.Reference. If the
842// referent has not yet been marked, put it on the appropriate list in
843// the gcHeap for later processing.
844void MarkSweep::DelayReferenceReferent(Object* obj) {
845 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700846 Class* klass = obj->GetClass();
847 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700848 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800849 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
850 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700851 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700852 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700853 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700854 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700855 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700856 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700857 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700858 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700859 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700860 list = &phantom_reference_list_;
861 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700862 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800863 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700864 }
865}
866
867// Scans the header and field references of a data object. If the
868// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700869// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700870inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700871#ifndef NDEBUG
872 ++other_count_;
873#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700874 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700875 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700876 DelayReferenceReferent(const_cast<Object*>(obj));
877 }
878}
879
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700880void MarkSweep::ScanRoot(const Object* obj) {
881 ScanObject(obj);
882}
883
Carl Shapiro69759ea2011-07-21 18:13:35 -0700884// Scans an object reference. Determines the type of the reference
885// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700886void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700887 DCHECK(obj != NULL);
888 DCHECK(obj->GetClass() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700889#ifndef NDEBUG
890 if (!IsMarked(obj)) {
891 heap_->DumpSpaces();
892 LOG(FATAL) << "Scanning unmarked object " << reinterpret_cast<const void*>(obj);
893 }
894#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700895 if (obj->IsClass()) {
896 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700897 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700898 ScanArray(obj);
899 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700900 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700901 }
902}
903
Ian Rogers5d76c432011-10-31 21:42:49 -0700904// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700905void MarkSweep::ProcessMarkStack() {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700906 if (kUseMarkStackPrefetch) {
907 const size_t fifo_size = 4;
908 const size_t fifo_mask = fifo_size - 1;
909 const Object* fifo[fifo_size];
910 for (size_t i = 0;i < fifo_size;++i) {
911 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700912 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700913 size_t fifo_pos = 0;
914 size_t fifo_count = 0;
915 for (;;) {
916 const Object* obj = fifo[fifo_pos & fifo_mask];
917 if (obj != NULL) {
918 ScanObject(obj);
919 fifo[fifo_pos & fifo_mask] = NULL;
920 --fifo_count;
921 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700922
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700923 if (!mark_stack_->IsEmpty()) {
924 const Object* obj = mark_stack_->PopBack();
925 DCHECK(obj != NULL);
926 fifo[fifo_pos & fifo_mask] = obj;
927 __builtin_prefetch(obj);
928 fifo_count++;
929 }
930 fifo_pos++;
931
932 if (!fifo_count) {
933 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
934 break;
935 }
936 }
937 } else {
938 while (!mark_stack_->IsEmpty()) {
939 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700940 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700941 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700942 }
943 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700944}
945
Carl Shapiro69759ea2011-07-21 18:13:35 -0700946// Walks the reference list marking any references subject to the
947// reference clearing policy. References with a black referent are
948// removed from the list. References with white referents biased
949// toward saving are blackened and also removed from the list.
950void MarkSweep::PreserveSomeSoftReferences(Object** list) {
951 DCHECK(list != NULL);
952 Object* clear = NULL;
953 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700954
955 DCHECK(mark_stack_->IsEmpty());
956
Carl Shapiro69759ea2011-07-21 18:13:35 -0700957 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800958 Object* ref = heap_->DequeuePendingReference(list);
959 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700960 if (referent == NULL) {
961 // Referent was cleared by the user during marking.
962 continue;
963 }
964 bool is_marked = IsMarked(referent);
965 if (!is_marked && ((++counter) & 1)) {
966 // Referent is white and biased toward saving, mark it.
967 MarkObject(referent);
968 is_marked = true;
969 }
970 if (!is_marked) {
971 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800972 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700973 }
974 }
975 *list = clear;
976 // Restart the mark with the newly black references added to the
977 // root set.
978 ProcessMarkStack();
979}
980
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700981inline bool MarkSweep::IsMarked(const Object* object) const
982 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
983 if (object >= immune_begin_ && object < immune_end_) {
984 return true;
985 }
986 DCHECK(current_mark_bitmap_ != NULL);
987 if (current_mark_bitmap_->HasAddress(object)) {
988 return current_mark_bitmap_->Test(object);
989 }
990 return heap_->GetMarkBitmap()->Test(object);
991}
992
993
Carl Shapiro69759ea2011-07-21 18:13:35 -0700994// Unlink the reference list clearing references objects with white
995// referents. Cleared references registered to a reference queue are
996// scheduled for appending by the heap worker thread.
997void MarkSweep::ClearWhiteReferences(Object** list) {
998 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700999 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001000 Object* ref = heap_->DequeuePendingReference(list);
1001 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001002 if (referent != NULL && !IsMarked(referent)) {
1003 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001004 heap_->ClearReferenceReferent(ref);
1005 if (heap_->IsEnqueuable(ref)) {
1006 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001007 }
1008 }
1009 }
1010 DCHECK(*list == NULL);
1011}
1012
1013// Enqueues finalizer references with white referents. White
1014// referents are blackened, moved to the zombie field, and the
1015// referent field is cleared.
1016void MarkSweep::EnqueueFinalizerReferences(Object** list) {
1017 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001018 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001019 bool has_enqueued = false;
1020 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001021 Object* ref = heap_->DequeuePendingReference(list);
1022 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001023 if (referent != NULL && !IsMarked(referent)) {
1024 MarkObject(referent);
1025 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001026 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001027 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001028 heap_->ClearReferenceReferent(ref);
1029 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001030 has_enqueued = true;
1031 }
1032 }
1033 if (has_enqueued) {
1034 ProcessMarkStack();
1035 }
1036 DCHECK(*list == NULL);
1037}
1038
Carl Shapiro58551df2011-07-24 03:09:51 -07001039// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001040void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1041 Object** weak_references,
1042 Object** finalizer_references,
1043 Object** phantom_references) {
1044 DCHECK(soft_references != NULL);
1045 DCHECK(weak_references != NULL);
1046 DCHECK(finalizer_references != NULL);
1047 DCHECK(phantom_references != NULL);
1048
1049 // Unless we are in the zygote or required to clear soft references
1050 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001051 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001052 PreserveSomeSoftReferences(soft_references);
1053 }
1054
1055 // Clear all remaining soft and weak references with white
1056 // referents.
1057 ClearWhiteReferences(soft_references);
1058 ClearWhiteReferences(weak_references);
1059
1060 // Preserve all white objects with finalize methods and schedule
1061 // them for finalization.
1062 EnqueueFinalizerReferences(finalizer_references);
1063
1064 // Clear all f-reachable soft and weak references with white
1065 // referents.
1066 ClearWhiteReferences(soft_references);
1067 ClearWhiteReferences(weak_references);
1068
1069 // Clear all phantom references with white referents.
1070 ClearWhiteReferences(phantom_references);
1071
1072 // At this point all reference lists should be empty.
1073 DCHECK(*soft_references == NULL);
1074 DCHECK(*weak_references == NULL);
1075 DCHECK(*finalizer_references == NULL);
1076 DCHECK(*phantom_references == NULL);
1077}
1078
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001079void MarkSweep::UnBindBitmaps() {
1080 const Spaces& spaces = heap_->GetSpaces();
1081 // TODO: C++0x auto
1082 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
1083 Space* space = *it;
1084 if (space->IsAllocSpace()) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001085 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001086 if (alloc_space->temp_bitmap_.get() != NULL) {
1087 // At this point, the temp_bitmap holds our old mark bitmap.
1088 SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
1089 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1090 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1091 alloc_space->mark_bitmap_.reset(new_bitmap);
1092 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1093 }
1094 }
1095 }
1096}
1097
Carl Shapiro69759ea2011-07-21 18:13:35 -07001098MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -07001099#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001100 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -07001101#endif
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001102 // Ensure that the mark stack is empty.
1103 CHECK(mark_stack_->IsEmpty());
1104
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001105 // Clear all of the alloc spaces' mark bitmaps.
1106 const Spaces& spaces = heap_->GetSpaces();
1107 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001108 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001109 ContinuousSpace* space = *it;
1110 if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) {
1111 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001112 }
1113 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001114 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001115
1116 // Reset the marked large objects.
1117 LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001118 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001119}
1120
1121} // namespace art