blob: 0869e26a0151ded04a335944a7f40b1aabfa65c2 [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 Chartier858f1c52012-10-17 17:45:55 -0700530class CheckpointMarkThreadRoots : public Thread::CheckpointFunction {
531 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();
539 DCHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc);
540 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
541 thread->VisitRoots(MarkSweep::MarkObjectVisitor, mark_sweep_);
542 mark_sweep_->GetBarrier().Pass(self);
543 }
544
545 private:
546 MarkSweep* mark_sweep_;
547};
548
549Barrier& MarkSweep::GetBarrier() {
550 return *gc_barrier_;
551}
552
553void MarkSweep::MarkRootsCheckpoint() {
554 UniquePtr<CheckpointMarkThreadRoots> check_point(new CheckpointMarkThreadRoots(this));
555 ThreadList* thread_list = Runtime::Current()->GetThreadList();
556 // Increment the count of the barrier. If all of the checkpoints have already been finished then
557 // will hit 0 and continue. Otherwise we are still waiting for some checkpoints, so the counter
558 // will go positive and we will unblock when it hits zero.
559 gc_barrier_->Increment(Thread::Current(), thread_list->RunCheckpoint(check_point.get()));
560}
561
Ian Rogers30fab402012-01-23 15:43:46 -0800562void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700563 size_t freed_objects = num_ptrs;
564 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800565 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700566 MarkSweep* mark_sweep = context->mark_sweep;
567 Heap* heap = mark_sweep->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800568 AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700569 Thread* self = context->self;
570 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700571 // Use a bulk free, that merges consecutive objects before freeing or free per object?
572 // Documentation suggests better free performance with merging, but this may be at the expensive
573 // of allocation.
574 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800575 static const bool kUseFreeList = true;
576 if (kUseFreeList) {
Ian Rogers30fab402012-01-23 15:43:46 -0800577 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700578 freed_bytes += space->FreeList(self, num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700579 } else {
580 for (size_t i = 0; i < num_ptrs; ++i) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700581 freed_bytes += space->Free(self, ptrs[i]);
Ian Rogers5d76c432011-10-31 21:42:49 -0700582 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700583 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700584
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700586 mark_sweep->freed_objects_ += freed_objects;
587 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700588}
589
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700590void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700591 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700592 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700593 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700594 // We don't free any actual memory to avoid dirtying the shared zygote pages.
595 for (size_t i = 0; i < num_ptrs; ++i) {
596 Object* obj = static_cast<Object*>(ptrs[i]);
597 heap->GetLiveBitmap()->Clear(obj);
598 heap->GetCardTable()->MarkCard(obj);
599 }
600}
601
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700602void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700603 size_t freed_bytes = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700604 DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700605
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700606 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
607 // bitmap, resulting in occasional frees of Weaks which are still in use.
608 // TODO: Fix when sweeping weaks works properly with mutators unpaused + allocation list.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700609 SweepSystemWeaksArray(allocations);
610 logger.AddSplit("SweepSystemWeaks");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700611
612 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
613 // going to free.
614 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
615 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700616 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
617 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
618 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700619 if (swap_bitmaps) {
620 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700621 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700622 }
623
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700624 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700625 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700626 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700627 Object** out = objects;
628
629 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700630 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700631 for (size_t i = 0;i < count;++i) {
632 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700633 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
634 if (LIKELY(mark_bitmap->HasAddress(obj))) {
635 if (!mark_bitmap->Test(obj)) {
636 // Don't bother un-marking since we clear the mark bitmap anyways.
637 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700638 }
639 } else if (!large_mark_objects->Test(obj)) {
640 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700641 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700642 }
643 }
644 logger.AddSplit("Process allocation stack");
645
646 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700647 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700648 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700649 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700650 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700651 freed_objects_ += freed_objects;
652 freed_bytes_ += freed_bytes;
653 logger.AddSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700654 allocations->Reset();
655 logger.AddSplit("Reset stack");
656}
657
658void MarkSweep::Sweep(bool partial, bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700659 DCHECK(mark_stack_->IsEmpty());
660
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700661 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
662 // bitmap, resulting in occasional frees of Weaks which are still in use.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700663 SweepSystemWeaks();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700664
Mathieu Chartier46a23632012-08-07 18:44:40 -0700665 const Spaces& spaces = heap_->GetSpaces();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800666 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700667 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700668 scc.self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700669 // TODO: C++0x auto
670 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700671 ContinuousSpace* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700672 if (
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700673 space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
674 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700675 ) {
676 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
677 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
678 scc.space = space->AsAllocSpace();
679 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
680 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700681 if (swap_bitmaps) {
682 std::swap(live_bitmap, mark_bitmap);
683 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700684 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700685 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700686 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
687 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700688 } else {
689 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700690 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
691 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700692 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700693 }
694 }
695}
696
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700697void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
698 // Sweep large objects
699 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700700 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
701 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
702 if (swap_bitmaps) {
703 std::swap(large_live_objects, large_mark_objects);
704 }
705 SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
706 // O(n*log(n)) but hopefully there are not too many large objects.
707 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700708 size_t freed_bytes = 0;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700709 // TODO: C++0x
Ian Rogers50b35e22012-10-04 10:09:15 -0700710 Thread* self = Thread::Current();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700711 for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) {
712 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700713 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700714 ++freed_objects;
715 }
716 }
717 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700718 freed_bytes_ += freed_bytes;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700719 // Large objects don't count towards bytes_allocated.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700720 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700721}
722
Carl Shapiro69759ea2011-07-21 18:13:35 -0700723// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700724inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700725 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700726 Class* klass = obj->GetClass();
727 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700728 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700729}
730
731// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700732inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700733 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700734 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700735}
736
Elliott Hughesb0663112011-10-19 18:16:37 -0700737inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700738 if (ref_offsets != CLASS_WALK_SUPER) {
739 // Found a reference offset bitmap. Mark the specified offsets.
740 while (ref_offsets != 0) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700741 const size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700742 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
743 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700744 MarkObject(ref);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700745 ref_offsets ^= CLASS_HIGH_BIT >> right_shift;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700746 }
747 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700748 // There is no reference offset bitmap. In the non-static case,
749 // walk up the class inheritance hierarchy and find reference
750 // offsets the hard way. In the static case, just consider this
751 // class.
752 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700753 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700754 klass = is_static ? NULL : klass->GetSuperClass()) {
755 size_t num_reference_fields = (is_static
756 ? klass->NumReferenceStaticFields()
757 : klass->NumReferenceInstanceFields());
758 for (size_t i = 0; i < num_reference_fields; ++i) {
759 Field* field = (is_static
760 ? klass->GetStaticField(i)
761 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700762 MemberOffset field_offset = field->GetOffset();
763 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700764 MarkObject(ref);
765 }
766 }
767 }
768}
769
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700770void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700771 const Spaces& spaces = heap_->GetSpaces();
772 // TODO: C++0x auto
773 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
774 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
775 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700776
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700777 bool is_marked = IsMarked(ref);
778 if (!is_marked) {
779 LOG(INFO) << **cur;
780 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
781 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
782 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
783 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700784
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700785 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
786 DCHECK(klass != NULL);
787 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
788 DCHECK(fields != NULL);
789 bool found = false;
790 for (int32_t i = 0; i < fields->GetLength(); ++i) {
791 const Field* cur = fields->Get(i);
792 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
793 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
794 found = true;
795 break;
796 }
797 }
798 if (!found) {
799 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
800 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700801
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700802 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
803 if (!obj_marked) {
804 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
805 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
806 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700807 }
808 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700809 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700810 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700811 }
812}
813
Carl Shapiro69759ea2011-07-21 18:13:35 -0700814// Scans the header, static field references, and interface pointers
815// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700816inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700817#ifndef NDEBUG
818 ++class_count_;
819#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700820 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700821 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700822}
823
824// Scans the header of all array objects. If the array object is
825// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700826inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700827#ifndef NDEBUG
828 ++array_count_;
829#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700830 MarkObject(obj->GetClass());
831 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700832 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700833 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700834 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700835 MarkObject(element);
836 }
837 }
838}
839
Carl Shapiro69759ea2011-07-21 18:13:35 -0700840// Process the "referent" field in a java.lang.ref.Reference. If the
841// referent has not yet been marked, put it on the appropriate list in
842// the gcHeap for later processing.
843void MarkSweep::DelayReferenceReferent(Object* obj) {
844 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700845 Class* klass = obj->GetClass();
846 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700847 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800848 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
849 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700850 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700851 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700852 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700853 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700854 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700855 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700856 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700857 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700858 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700859 list = &phantom_reference_list_;
860 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700861 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800862 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700863 }
864}
865
866// Scans the header and field references of a data object. If the
867// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700868// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700869inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700870#ifndef NDEBUG
871 ++other_count_;
872#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700873 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700874 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700875 DelayReferenceReferent(const_cast<Object*>(obj));
876 }
877}
878
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700879void MarkSweep::ScanRoot(const Object* obj) {
880 ScanObject(obj);
881}
882
Carl Shapiro69759ea2011-07-21 18:13:35 -0700883// Scans an object reference. Determines the type of the reference
884// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700885void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700886 DCHECK(obj != NULL);
887 DCHECK(obj->GetClass() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700888#ifndef NDEBUG
889 if (!IsMarked(obj)) {
890 heap_->DumpSpaces();
891 LOG(FATAL) << "Scanning unmarked object " << reinterpret_cast<const void*>(obj);
892 }
893#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700894 if (obj->IsClass()) {
895 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700896 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700897 ScanArray(obj);
898 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700899 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700900 }
901}
902
Ian Rogers5d76c432011-10-31 21:42:49 -0700903// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700904void MarkSweep::ProcessMarkStack() {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700905 if (kUseMarkStackPrefetch) {
906 const size_t fifo_size = 4;
907 const size_t fifo_mask = fifo_size - 1;
908 const Object* fifo[fifo_size];
909 for (size_t i = 0;i < fifo_size;++i) {
910 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700911 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700912 size_t fifo_pos = 0;
913 size_t fifo_count = 0;
914 for (;;) {
915 const Object* obj = fifo[fifo_pos & fifo_mask];
916 if (obj != NULL) {
917 ScanObject(obj);
918 fifo[fifo_pos & fifo_mask] = NULL;
919 --fifo_count;
920 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700921
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700922 if (!mark_stack_->IsEmpty()) {
923 const Object* obj = mark_stack_->PopBack();
924 DCHECK(obj != NULL);
925 fifo[fifo_pos & fifo_mask] = obj;
926 __builtin_prefetch(obj);
927 fifo_count++;
928 }
929 fifo_pos++;
930
931 if (!fifo_count) {
932 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
933 break;
934 }
935 }
936 } else {
937 while (!mark_stack_->IsEmpty()) {
938 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700939 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700940 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700941 }
942 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700943}
944
Carl Shapiro69759ea2011-07-21 18:13:35 -0700945// Walks the reference list marking any references subject to the
946// reference clearing policy. References with a black referent are
947// removed from the list. References with white referents biased
948// toward saving are blackened and also removed from the list.
949void MarkSweep::PreserveSomeSoftReferences(Object** list) {
950 DCHECK(list != NULL);
951 Object* clear = NULL;
952 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700953
954 DCHECK(mark_stack_->IsEmpty());
955
Carl Shapiro69759ea2011-07-21 18:13:35 -0700956 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800957 Object* ref = heap_->DequeuePendingReference(list);
958 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700959 if (referent == NULL) {
960 // Referent was cleared by the user during marking.
961 continue;
962 }
963 bool is_marked = IsMarked(referent);
964 if (!is_marked && ((++counter) & 1)) {
965 // Referent is white and biased toward saving, mark it.
966 MarkObject(referent);
967 is_marked = true;
968 }
969 if (!is_marked) {
970 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800971 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700972 }
973 }
974 *list = clear;
975 // Restart the mark with the newly black references added to the
976 // root set.
977 ProcessMarkStack();
978}
979
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700980inline bool MarkSweep::IsMarked(const Object* object) const
981 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
982 if (object >= immune_begin_ && object < immune_end_) {
983 return true;
984 }
985 DCHECK(current_mark_bitmap_ != NULL);
986 if (current_mark_bitmap_->HasAddress(object)) {
987 return current_mark_bitmap_->Test(object);
988 }
989 return heap_->GetMarkBitmap()->Test(object);
990}
991
992
Carl Shapiro69759ea2011-07-21 18:13:35 -0700993// Unlink the reference list clearing references objects with white
994// referents. Cleared references registered to a reference queue are
995// scheduled for appending by the heap worker thread.
996void MarkSweep::ClearWhiteReferences(Object** list) {
997 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700998 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800999 Object* ref = heap_->DequeuePendingReference(list);
1000 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001001 if (referent != NULL && !IsMarked(referent)) {
1002 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001003 heap_->ClearReferenceReferent(ref);
1004 if (heap_->IsEnqueuable(ref)) {
1005 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001006 }
1007 }
1008 }
1009 DCHECK(*list == NULL);
1010}
1011
1012// Enqueues finalizer references with white referents. White
1013// referents are blackened, moved to the zombie field, and the
1014// referent field is cleared.
1015void MarkSweep::EnqueueFinalizerReferences(Object** list) {
1016 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001017 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001018 bool has_enqueued = false;
1019 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001020 Object* ref = heap_->DequeuePendingReference(list);
1021 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001022 if (referent != NULL && !IsMarked(referent)) {
1023 MarkObject(referent);
1024 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001025 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001026 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001027 heap_->ClearReferenceReferent(ref);
1028 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001029 has_enqueued = true;
1030 }
1031 }
1032 if (has_enqueued) {
1033 ProcessMarkStack();
1034 }
1035 DCHECK(*list == NULL);
1036}
1037
Carl Shapiro58551df2011-07-24 03:09:51 -07001038// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001039void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1040 Object** weak_references,
1041 Object** finalizer_references,
1042 Object** phantom_references) {
1043 DCHECK(soft_references != NULL);
1044 DCHECK(weak_references != NULL);
1045 DCHECK(finalizer_references != NULL);
1046 DCHECK(phantom_references != NULL);
1047
1048 // Unless we are in the zygote or required to clear soft references
1049 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001050 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001051 PreserveSomeSoftReferences(soft_references);
1052 }
1053
1054 // Clear all remaining soft and weak references with white
1055 // referents.
1056 ClearWhiteReferences(soft_references);
1057 ClearWhiteReferences(weak_references);
1058
1059 // Preserve all white objects with finalize methods and schedule
1060 // them for finalization.
1061 EnqueueFinalizerReferences(finalizer_references);
1062
1063 // Clear all f-reachable soft and weak references with white
1064 // referents.
1065 ClearWhiteReferences(soft_references);
1066 ClearWhiteReferences(weak_references);
1067
1068 // Clear all phantom references with white referents.
1069 ClearWhiteReferences(phantom_references);
1070
1071 // At this point all reference lists should be empty.
1072 DCHECK(*soft_references == NULL);
1073 DCHECK(*weak_references == NULL);
1074 DCHECK(*finalizer_references == NULL);
1075 DCHECK(*phantom_references == NULL);
1076}
1077
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001078void MarkSweep::UnBindBitmaps() {
1079 const Spaces& spaces = heap_->GetSpaces();
1080 // TODO: C++0x auto
1081 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
1082 Space* space = *it;
1083 if (space->IsAllocSpace()) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001084 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001085 if (alloc_space->temp_bitmap_.get() != NULL) {
1086 // At this point, the temp_bitmap holds our old mark bitmap.
1087 SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
1088 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1089 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1090 alloc_space->mark_bitmap_.reset(new_bitmap);
1091 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1092 }
1093 }
1094 }
1095}
1096
Carl Shapiro69759ea2011-07-21 18:13:35 -07001097MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -07001098#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001099 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -07001100#endif
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001101 // Ensure that the mark stack is empty.
1102 CHECK(mark_stack_->IsEmpty());
1103
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001104 // Clear all of the alloc spaces' mark bitmaps.
1105 const Spaces& spaces = heap_->GetSpaces();
1106 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001107 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001108 ContinuousSpace* space = *it;
1109 if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) {
1110 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001111 }
1112 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001113 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001114
1115 // Reset the marked large objects.
1116 LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001117 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001118}
1119
1120} // namespace art