blob: 86373702bf619c2fcc585c0fc90d693e6c839b69 [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"
Carl Shapiro69759ea2011-07-21 18:13:35 -070040
Carl Shapiro69759ea2011-07-21 18:13:35 -070041namespace art {
42
Mathieu Chartier858f1c52012-10-17 17:45:55 -070043static const bool kUseMarkStackPrefetch = true;
44
Mathieu Chartier357e9be2012-08-01 11:00:14 -070045class SetFingerVisitor {
46 public:
47 SetFingerVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
48
49 }
50
51 void operator ()(void* finger) const {
52 mark_sweep_->SetFinger(reinterpret_cast<Object*>(finger));
53 }
54
55 private:
56 MarkSweep* const mark_sweep_;
57};
58
Mathieu Chartierd8195f12012-10-05 12:21:28 -070059MarkSweep::MarkSweep(ObjectStack* mark_stack)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070060 : current_mark_bitmap_(NULL),
61 mark_stack_(mark_stack),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070062 heap_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070063 finger_(NULL),
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070064 immune_begin_(NULL),
65 immune_end_(NULL),
Mathieu Chartier5301cd22012-05-31 12:11:36 -070066 soft_reference_list_(NULL),
67 weak_reference_list_(NULL),
68 finalizer_reference_list_(NULL),
69 phantom_reference_list_(NULL),
70 cleared_reference_list_(NULL),
Mathieu Chartier357e9be2012-08-01 11:00:14 -070071 freed_bytes_(0), freed_objects_(0),
Mathieu Chartier858f1c52012-10-17 17:45:55 -070072 class_count_(0), array_count_(0), other_count_(0),
73 gc_barrier_(new Barrier) {
Mathieu Chartier5301cd22012-05-31 12:11:36 -070074 DCHECK(mark_stack_ != NULL);
75}
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080076
Mathieu Chartier5301cd22012-05-31 12:11:36 -070077void MarkSweep::Init() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080078 heap_ = Runtime::Current()->GetHeap();
Mathieu Chartier5301cd22012-05-31 12:11:36 -070079 mark_stack_->Reset();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070080 // TODO: C++0x auto
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070081 FindDefaultMarkBitmap();
buzbee0d966cf2011-09-08 17:34:58 -070082 // TODO: if concurrent, enable card marking in compiler
Carl Shapiro58551df2011-07-24 03:09:51 -070083 // TODO: check that the mark bitmap is entirely clear.
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -070084 // Mark any concurrent roots as dirty since we need to scan them at least once during this GC.
85 Runtime::Current()->DirtyRoots();
Carl Shapiro58551df2011-07-24 03:09:51 -070086}
87
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070088void MarkSweep::FindDefaultMarkBitmap() {
89 const Spaces& spaces = heap_->GetSpaces();
90 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
91 if ((*it)->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
92 current_mark_bitmap_ = (*it)->GetMarkBitmap();
93 CHECK(current_mark_bitmap_ != NULL);
94 return;
95 }
96 }
97 GetHeap()->DumpSpaces();
98 LOG(FATAL) << "Could not find a default mark bitmap";
99}
100
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700101inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700102 DCHECK(obj != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700103
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700104 if (obj >= immune_begin_ && obj < immune_end_) {
105 DCHECK(IsMarked(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700106 return;
107 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700108
109 // Try to take advantage of locality of references within a space, failing this find the space
110 // the hard way.
Ian Rogers506de0c2012-09-17 15:39:06 -0700111 if (UNLIKELY(!current_mark_bitmap_->HasAddress(obj))) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700112 SpaceBitmap* new_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(obj);
113 if (new_bitmap != NULL) {
114 current_mark_bitmap_ = new_bitmap;
115 } else {
116 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
117 SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
118 if (!large_objects->Test(obj)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700119 if (!large_object_space->Contains(obj)) {
120 LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
121 LOG(ERROR) << "Attempting see if it's a bad root";
122 VerifyRoots();
123 LOG(FATAL) << "Can't mark bad root";
124 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700125 large_objects->Set(obj);
126 // Don't need to check finger since large objects never have any object references.
127 }
128 // TODO: Improve clarity of control flow in this function?
129 return;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700130 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700131 }
132
Carl Shapiro69759ea2011-07-21 18:13:35 -0700133 // This object was not previously marked.
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700134 if (!current_mark_bitmap_->Test(obj)) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700135 current_mark_bitmap_->Set(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700136 if (check_finger && obj < finger_) {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700137 // Do we need to expand the mark stack?
138 if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) {
139 std::vector<Object*> temp;
140 temp.insert(temp.begin(), mark_stack_->Begin(), mark_stack_->End());
141 mark_stack_->Resize(mark_stack_->Capacity() * 2);
142 for (size_t i = 0; i < temp.size(); ++i) {
143 mark_stack_->PushBack(temp[i]);
144 }
145 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700146 // The object must be pushed on to the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700147 mark_stack_->PushBack(const_cast<Object*>(obj));
Carl Shapiro69759ea2011-07-21 18:13:35 -0700148 }
149 }
150}
151
152// Used to mark objects when recursing. Recursion is done by moving
153// the finger across the bitmaps in address order and marking child
154// objects. Any newly-marked objects whose addresses are lower than
155// the finger won't be visited by the bitmap scan, so those objects
156// need to be added to the mark stack.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700157void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700158 if (obj != NULL) {
159 MarkObject0(obj, true);
160 }
161}
162
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700163void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700164 DCHECK(root != NULL);
165 DCHECK(arg != NULL);
166 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Ian Rogers5d76c432011-10-31 21:42:49 -0700167 mark_sweep->MarkObject0(root, false);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700168}
169
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700170void MarkSweep::ReMarkObjectVisitor(const Object* root, void* arg) {
171 DCHECK(root != NULL);
172 DCHECK(arg != NULL);
173 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
174 mark_sweep->MarkObject0(root, true);
175}
176
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700177void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
178 const AbstractMethod* method) {
179 reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, method);
180}
181
182void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const AbstractMethod* method) {
183 // See if the root is on any space bitmap.
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700184 if (GetHeap()->GetLiveBitmap()->GetSpaceBitmap(root) == NULL) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700185 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartier4202b742012-10-17 17:51:25 -0700186 if (!large_object_space->Contains(root)) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700187 LOG(ERROR) << "Found invalid root: " << root;
188 LOG(ERROR) << "VReg / Shadow frame offset: " << vreg;
189 if (method != NULL) {
190 LOG(ERROR) << "In method " << PrettyMethod(method, true);
191 }
192 }
193 }
194}
195
196void MarkSweep::VerifyRoots() {
197 Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
198}
199
Carl Shapiro69759ea2011-07-21 18:13:35 -0700200// Marks all objects in the root set.
201void MarkSweep::MarkRoots() {
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700202 Runtime::Current()->VisitNonConcurrentRoots(MarkObjectVisitor, this);
203}
204
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700205void MarkSweep::MarkNonThreadRoots() {
206 Runtime::Current()->VisitNonThreadRoots(MarkObjectVisitor, this);
207}
208
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700209void MarkSweep::MarkConcurrentRoots() {
210 Runtime::Current()->VisitConcurrentRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700211}
212
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700213class CheckObjectVisitor {
214 public:
215 CheckObjectVisitor(MarkSweep* const mark_sweep)
216 : mark_sweep_(mark_sweep) {
217
218 }
219
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 void operator ()(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700221 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
222 Locks::mutator_lock_) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700223 mark_sweep_->CheckReference(obj, ref, offset, is_static);
224 }
225
226 private:
227 MarkSweep* const mark_sweep_;
228};
229
230void MarkSweep::CheckObject(const Object* obj) {
231 DCHECK(obj != NULL);
232 CheckObjectVisitor visitor(this);
233 VisitObjectReferences(obj, visitor);
234}
235
236void MarkSweep::VerifyImageRootVisitor(Object* root, void* arg) {
237 DCHECK(root != NULL);
238 DCHECK(arg != NULL);
239 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700240 DCHECK(mark_sweep->heap_->GetMarkBitmap()->Test(root));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700241 mark_sweep->CheckObject(root);
242}
243
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700244void MarkSweep::CopyMarkBits(ContinuousSpace* space) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700245 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
246 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
247 mark_bitmap->CopyFrom(live_bitmap);
Ian Rogers5d76c432011-10-31 21:42:49 -0700248}
249
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700250void MarkSweep::BindLiveToMarkBitmap(ContinuousSpace* space) {
251 CHECK(space->IsAllocSpace());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700252 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700253 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
254 SpaceBitmap* mark_bitmap = alloc_space->mark_bitmap_.release();
255 GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
256 alloc_space->temp_bitmap_.reset(mark_bitmap);
257 alloc_space->mark_bitmap_.reset(live_bitmap);
258}
259
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700260class ScanImageRootVisitor {
261 public:
262 ScanImageRootVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700263 }
264
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 void operator ()(const Object* root) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700268 DCHECK(root != NULL);
269 mark_sweep_->ScanObject(root);
270 }
271
272 private:
273 MarkSweep* const mark_sweep_;
274};
275
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700276void MarkSweep::ScanGrayObjects(bool update_finger) {
277 const Spaces& spaces = heap_->GetSpaces();
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700278 CardTable* card_table = heap_->GetCardTable();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700279 ScanImageRootVisitor image_root_visitor(this);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700280 SetFingerVisitor finger_visitor(this);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700281 // TODO: C++ 0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700282 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700283 ContinuousSpace* space = *it;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700284 byte* begin = space->Begin();
285 byte* end = space->End();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700286 // Image spaces are handled properly since live == marked for them.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700287 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
288 if (update_finger) {
289 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, finger_visitor);
290 } else {
291 card_table->Scan(mark_bitmap, begin, end, image_root_visitor, IdentityFunctor());
292 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700293 }
294}
295
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700296class CheckBitmapVisitor {
297 public:
298 CheckBitmapVisitor(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
299
300 }
301
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700303 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
304 Locks::mutator_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700305 DCHECK(obj != NULL);
306 mark_sweep_->CheckObject(obj);
307 }
308
309 private:
310 MarkSweep* mark_sweep_;
311};
312
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700313void MarkSweep::VerifyImageRoots() {
314 // Verify roots ensures that all the references inside the image space point
315 // objects which are either in the image space or marked objects in the alloc
316 // space
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700317 CheckBitmapVisitor visitor(this);
318 const Spaces& spaces = heap_->GetSpaces();
319 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700320 if ((*it)->IsImageSpace()) {
321 ImageSpace* space = (*it)->AsImageSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700322 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
323 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
324 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700325 DCHECK(live_bitmap != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700326 live_bitmap->VisitMarkedRange(begin, end, visitor, IdentityFunctor());
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700327 }
328 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700329}
330
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700331class ScanObjectVisitor {
332 public:
333 ScanObjectVisitor(MarkSweep* const mark_sweep) : mark_sweep_(mark_sweep) {
334
335 }
336
337 void operator ()(const Object* obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700338 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
339 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700340 mark_sweep_->ScanObject(obj);
341 }
342
343 private:
344 MarkSweep* const mark_sweep_;
345};
346
Carl Shapiro58551df2011-07-24 03:09:51 -0700347// Populates the mark stack based on the set of marked objects and
348// recursively marks until the mark stack is emptied.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700349void MarkSweep::RecursiveMark(bool partial, TimingLogger& timings) {
Brian Carlstrom1f870082011-08-23 16:02:11 -0700350 // RecursiveMark will build the lists of known instances of the Reference classes.
351 // See DelayReferenceReferent for details.
352 CHECK(soft_reference_list_ == NULL);
353 CHECK(weak_reference_list_ == NULL);
354 CHECK(finalizer_reference_list_ == NULL);
355 CHECK(phantom_reference_list_ == NULL);
356 CHECK(cleared_reference_list_ == NULL);
357
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700358 const Spaces& spaces = heap_->GetSpaces();
359
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700360 SetFingerVisitor set_finger_visitor(this);
361 ScanObjectVisitor scan_visitor(this);
362 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700363 ContinuousSpace* space = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700364 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
365 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700366 ) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700367 current_mark_bitmap_ = space->GetMarkBitmap();
368 if (current_mark_bitmap_ == NULL) {
369 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700370 LOG(FATAL) << "invalid bitmap";
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700371 }
372 // This function does not handle heap end increasing, so we must use the space end.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700373 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
374 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700375 current_mark_bitmap_->VisitMarkedRange(begin, end, scan_visitor, set_finger_visitor);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700376 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700377 }
378 finger_ = reinterpret_cast<Object*>(~0);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700379 timings.AddSplit("RecursiveMark");
Ian Rogers5d76c432011-10-31 21:42:49 -0700380 // TODO: tune the frequency of emptying the mark stack
Carl Shapiro58551df2011-07-24 03:09:51 -0700381 ProcessMarkStack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700382 timings.AddSplit("ProcessMarkStack");
Carl Shapiro58551df2011-07-24 03:09:51 -0700383}
384
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700385void MarkSweep::RecursiveMarkCards(CardTable* card_table, const std::vector<byte*>& cards,
386 TimingLogger& timings) {
387 ScanImageRootVisitor image_root_visitor(this);
388 SetFingerVisitor finger_visitor(this);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700389 const size_t card_count = cards.size();
390 SpaceBitmap* active_bitmap = NULL;
391 for (size_t i = 0;i < card_count;) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700392 Object* start_obj = reinterpret_cast<Object*>(card_table->AddrFromCard(cards[i]));
393 uintptr_t begin = reinterpret_cast<uintptr_t>(start_obj);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700394 uintptr_t end = begin + CardTable::kCardSize;
395 for (++i; reinterpret_cast<uintptr_t>(cards[i]) == end && i < card_count; ++i) {
396 end += CardTable::kCardSize;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700397 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700398 if (active_bitmap == NULL || !active_bitmap->HasAddress(start_obj)) {
399 active_bitmap = heap_->GetMarkBitmap()->GetSpaceBitmap(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700400#ifndef NDEBUG
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700401 if (active_bitmap == NULL) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700402 GetHeap()->DumpSpaces();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700403 LOG(FATAL) << "Object " << reinterpret_cast<const void*>(start_obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700404 }
405#endif
406 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700407 active_bitmap->VisitMarkedRange(begin, end, image_root_visitor, finger_visitor);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700408 }
409 timings.AddSplit("RecursiveMarkCards");
410 ProcessMarkStack();
411 timings.AddSplit("ProcessMarkStack");
412}
413
414bool MarkSweep::IsMarkedCallback(const Object* object, void* arg) {
415 return
416 reinterpret_cast<MarkSweep*>(arg)->IsMarked(object) ||
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700417 !reinterpret_cast<MarkSweep*>(arg)->GetHeap()->GetLiveBitmap()->Test(object);
418}
419
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700420void MarkSweep::RecursiveMarkDirtyObjects(bool update_finger) {
421 ScanGrayObjects(update_finger);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700422 ProcessMarkStack();
423}
424
Carl Shapiro58551df2011-07-24 03:09:51 -0700425void MarkSweep::ReMarkRoots() {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700426 Runtime::Current()->VisitRoots(ReMarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700427}
428
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700429void MarkSweep::SweepJniWeakGlobals(Heap::IsMarkedTester is_marked, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700430 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700431 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700432 IndirectReferenceTable* table = &vm->weak_globals;
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700433 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -0700434 for (It it = table->begin(), end = table->end(); it != end; ++it) {
435 const Object** entry = *it;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700436 if (!is_marked(*entry, arg)) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700437 *entry = kClearedJniWeakGlobal;
438 }
439 }
440}
441
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700442struct ArrayMarkedCheck {
443 ObjectStack* live_stack;
444 MarkSweep* mark_sweep;
445};
446
447// Either marked or not live.
448bool MarkSweep::IsMarkedArrayCallback(const Object* object, void* arg) {
449 ArrayMarkedCheck* array_check = reinterpret_cast<ArrayMarkedCheck*>(arg);
450 if (array_check->mark_sweep->IsMarked(object)) {
451 return true;
452 }
453 ObjectStack* live_stack = array_check->live_stack;
454 return std::find(live_stack->Begin(), live_stack->End(), object) == live_stack->End();
455}
456
457void MarkSweep::SweepSystemWeaksArray(ObjectStack* allocations) {
Mathieu Chartier46a23632012-08-07 18:44:40 -0700458 Runtime* runtime = Runtime::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700459 // The callbacks check
460 // !is_marked where is_marked is the callback but we want
461 // !IsMarked && IsLive
462 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
463 // Or for swapped (IsLive || !IsMarked).
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700464
465 ArrayMarkedCheck visitor;
466 visitor.live_stack = allocations;
467 visitor.mark_sweep = this;
468 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedArrayCallback, &visitor);
469 runtime->GetMonitorList()->SweepMonitorList(IsMarkedArrayCallback, &visitor);
470 SweepJniWeakGlobals(IsMarkedArrayCallback, &visitor);
471}
472
473void MarkSweep::SweepSystemWeaks() {
474 Runtime* runtime = Runtime::Current();
475 // The callbacks check
476 // !is_marked where is_marked is the callback but we want
477 // !IsMarked && IsLive
478 // So compute !(!IsMarked && IsLive) which is equal to (IsMarked || !IsLive).
479 // Or for swapped (IsLive || !IsMarked).
480 runtime->GetInternTable()->SweepInternTableWeaks(IsMarkedCallback, this);
481 runtime->GetMonitorList()->SweepMonitorList(IsMarkedCallback, this);
482 SweepJniWeakGlobals(IsMarkedCallback, this);
Carl Shapiro58551df2011-07-24 03:09:51 -0700483}
484
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700485bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
486 reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
487 // We don't actually want to sweep the object, so lets return "marked"
488 return true;
489}
490
491void MarkSweep::VerifyIsLive(const Object* obj) {
492 Heap* heap = GetHeap();
493 if (!heap->GetLiveBitmap()->Test(obj)) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700494 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
495 if (!large_object_space->GetLiveObjects()->Test(obj)) {
496 if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
497 heap->allocation_stack_->End()) {
498 // Object not found!
499 heap->DumpSpaces();
500 LOG(FATAL) << "Found dead object " << obj;
501 }
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700502 }
503 }
504}
505
506void MarkSweep::VerifySystemWeaks() {
507 Runtime* runtime = Runtime::Current();
508 // Verify system weaks, uses a special IsMarked callback which always returns true.
509 runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
510 runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
511
512 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers50b35e22012-10-04 10:09:15 -0700513 MutexLock mu(Thread::Current(), vm->weak_globals_lock);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700514 IndirectReferenceTable* table = &vm->weak_globals;
515 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
516 for (It it = table->begin(), end = table->end(); it != end; ++it) {
517 const Object** entry = *it;
518 VerifyIsLive(*entry);
519 }
520}
521
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800522struct SweepCallbackContext {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700523 MarkSweep* mark_sweep;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800524 AllocSpace* space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700525 Thread* self;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800526};
527
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700528class CheckpointMarkThreadRoots : public Thread::CheckpointFunction {
529 public:
530 CheckpointMarkThreadRoots(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) {
531
532 }
533
534 virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
535 // Note: self is not necessarily equal to thread since thread may be suspended.
536 Thread* self = Thread::Current();
537 DCHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc);
538 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
539 thread->VisitRoots(MarkSweep::MarkObjectVisitor, mark_sweep_);
540 mark_sweep_->GetBarrier().Pass(self);
541 }
542
543 private:
544 MarkSweep* mark_sweep_;
545};
546
547Barrier& MarkSweep::GetBarrier() {
548 return *gc_barrier_;
549}
550
551void MarkSweep::MarkRootsCheckpoint() {
552 UniquePtr<CheckpointMarkThreadRoots> check_point(new CheckpointMarkThreadRoots(this));
553 ThreadList* thread_list = Runtime::Current()->GetThreadList();
554 // Increment the count of the barrier. If all of the checkpoints have already been finished then
555 // will hit 0 and continue. Otherwise we are still waiting for some checkpoints, so the counter
556 // will go positive and we will unblock when it hits zero.
557 gc_barrier_->Increment(Thread::Current(), thread_list->RunCheckpoint(check_point.get()));
558}
559
Ian Rogers30fab402012-01-23 15:43:46 -0800560void MarkSweep::SweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700561 size_t freed_objects = num_ptrs;
562 size_t freed_bytes = 0;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800563 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700564 MarkSweep* mark_sweep = context->mark_sweep;
565 Heap* heap = mark_sweep->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800566 AllocSpace* space = context->space;
Ian Rogers50b35e22012-10-04 10:09:15 -0700567 Thread* self = context->self;
568 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
Ian Rogers5d76c432011-10-31 21:42:49 -0700569 // Use a bulk free, that merges consecutive objects before freeing or free per object?
570 // Documentation suggests better free performance with merging, but this may be at the expensive
571 // of allocation.
572 // TODO: investigate performance
Ian Rogers30fab402012-01-23 15:43:46 -0800573 static const bool kUseFreeList = true;
574 if (kUseFreeList) {
Ian Rogers30fab402012-01-23 15:43:46 -0800575 // AllocSpace::FreeList clears the value in ptrs, so perform after clearing the live bit
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700576 freed_bytes += space->FreeList(self, num_ptrs, ptrs);
Ian Rogers5d76c432011-10-31 21:42:49 -0700577 } else {
578 for (size_t i = 0; i < num_ptrs; ++i) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700579 freed_bytes += space->Free(self, ptrs[i]);
Ian Rogers5d76c432011-10-31 21:42:49 -0700580 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700581 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700582
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700583 heap->RecordFree(freed_objects, freed_bytes);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700584 mark_sweep->freed_objects_ += freed_objects;
585 mark_sweep->freed_bytes_ += freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700586}
587
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700588void MarkSweep::ZygoteSweepCallback(size_t num_ptrs, Object** ptrs, void* arg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700589 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Ian Rogers50b35e22012-10-04 10:09:15 -0700590 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700591 Heap* heap = context->mark_sweep->GetHeap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700592 // We don't free any actual memory to avoid dirtying the shared zygote pages.
593 for (size_t i = 0; i < num_ptrs; ++i) {
594 Object* obj = static_cast<Object*>(ptrs[i]);
595 heap->GetLiveBitmap()->Clear(obj);
596 heap->GetCardTable()->MarkCard(obj);
597 }
598}
599
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700600void MarkSweep::SweepArray(TimingLogger& logger, ObjectStack* allocations, bool swap_bitmaps) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700601 size_t freed_bytes = 0;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700602 DlMallocSpace* space = heap_->GetAllocSpace();
Elliott Hughes2da50362011-10-10 16:57:08 -0700603
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700604 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
605 // bitmap, resulting in occasional frees of Weaks which are still in use.
606 // TODO: Fix when sweeping weaks works properly with mutators unpaused + allocation list.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700607 SweepSystemWeaksArray(allocations);
608 logger.AddSplit("SweepSystemWeaks");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700609
610 // Newly allocated objects MUST be in the alloc space and those are the only objects which we are
611 // going to free.
612 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
613 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700614 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
615 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
616 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700617 if (swap_bitmaps) {
618 std::swap(live_bitmap, mark_bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700619 std::swap(large_live_objects, large_mark_objects);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700620 }
621
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700622 size_t freed_large_objects = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700623 size_t count = allocations->Size();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700624 Object** objects = const_cast<Object**>(allocations->Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700625 Object** out = objects;
626
627 // Empty the allocation stack.
Ian Rogers50b35e22012-10-04 10:09:15 -0700628 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700629 for (size_t i = 0;i < count;++i) {
630 Object* obj = objects[i];
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700631 // There should only be objects in the AllocSpace/LargeObjectSpace in the allocation stack.
632 if (LIKELY(mark_bitmap->HasAddress(obj))) {
633 if (!mark_bitmap->Test(obj)) {
634 // Don't bother un-marking since we clear the mark bitmap anyways.
635 *(out++) = obj;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700636 }
637 } else if (!large_mark_objects->Test(obj)) {
638 ++freed_large_objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700639 freed_bytes += large_object_space->Free(self, obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700640 }
641 }
642 logger.AddSplit("Process allocation stack");
643
644 size_t freed_objects = out - objects;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700645 freed_bytes += space->FreeList(self, freed_objects, objects);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700646 VLOG(heap) << "Freed " << freed_objects << "/" << count
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700647 << " objects with size " << PrettySize(freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700648 heap_->RecordFree(freed_objects + freed_large_objects, freed_bytes);
Mathieu Chartier40e978b2012-09-07 11:38:36 -0700649 freed_objects_ += freed_objects;
650 freed_bytes_ += freed_bytes;
651 logger.AddSplit("FreeList");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700652 allocations->Reset();
653 logger.AddSplit("Reset stack");
654}
655
656void MarkSweep::Sweep(bool partial, bool swap_bitmaps) {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700657 DCHECK(mark_stack_->IsEmpty());
658
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700659 // If we don't swap bitmaps then newly allocated Weaks go into the live bitmap but not mark
660 // bitmap, resulting in occasional frees of Weaks which are still in use.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700661 SweepSystemWeaks();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700662
Mathieu Chartier46a23632012-08-07 18:44:40 -0700663 const Spaces& spaces = heap_->GetSpaces();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800664 SweepCallbackContext scc;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700665 scc.mark_sweep = this;
Ian Rogers50b35e22012-10-04 10:09:15 -0700666 scc.self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700667 // TODO: C++0x auto
668 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700669 ContinuousSpace* space = *it;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700670 if (
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700671 space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect ||
672 (!partial && space->GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700673 ) {
674 uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin());
675 uintptr_t end = reinterpret_cast<uintptr_t>(space->End());
676 scc.space = space->AsAllocSpace();
677 SpaceBitmap* live_bitmap = space->GetLiveBitmap();
678 SpaceBitmap* mark_bitmap = space->GetMarkBitmap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700679 if (swap_bitmaps) {
680 std::swap(live_bitmap, mark_bitmap);
681 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700682 if (space->GetGcRetentionPolicy() == kGcRetentionPolicyAlwaysCollect) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700683 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700684 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
685 &SweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700686 } else {
687 // Zygote sweep takes care of dirtying cards and clearing live bits, does not free actual memory.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700688 SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap, begin, end,
689 &ZygoteSweepCallback, reinterpret_cast<void*>(&scc));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700690 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700691 }
692 }
693}
694
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700695void MarkSweep::SweepLargeObjects(bool swap_bitmaps) {
696 // Sweep large objects
697 LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700698 SpaceSetMap* large_live_objects = large_object_space->GetLiveObjects();
699 SpaceSetMap* large_mark_objects = large_object_space->GetMarkObjects();
700 if (swap_bitmaps) {
701 std::swap(large_live_objects, large_mark_objects);
702 }
703 SpaceSetMap::Objects& live_objects = large_live_objects->GetObjects();
704 // O(n*log(n)) but hopefully there are not too many large objects.
705 size_t freed_objects = 0;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700706 size_t freed_bytes = 0;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700707 // TODO: C++0x
Ian Rogers50b35e22012-10-04 10:09:15 -0700708 Thread* self = Thread::Current();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700709 for (SpaceSetMap::Objects::iterator it = live_objects.begin(); it != live_objects.end(); ++it) {
710 if (!large_mark_objects->Test(*it)) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700711 freed_bytes += large_object_space->Free(self, const_cast<Object*>(*it));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700712 ++freed_objects;
713 }
714 }
715 freed_objects_ += freed_objects;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700716 freed_bytes_ += freed_bytes;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700717 // Large objects don't count towards bytes_allocated.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700718 GetHeap()->RecordFree(freed_objects, freed_bytes);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700719}
720
Carl Shapiro69759ea2011-07-21 18:13:35 -0700721// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700722inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700723 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700724 Class* klass = obj->GetClass();
725 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700726 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700727}
728
729// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700730inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700731 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700732 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700733}
734
Elliott Hughesb0663112011-10-19 18:16:37 -0700735inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700736 if (ref_offsets != CLASS_WALK_SUPER) {
737 // Found a reference offset bitmap. Mark the specified offsets.
738 while (ref_offsets != 0) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700739 const size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700740 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
741 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700742 MarkObject(ref);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700743 ref_offsets ^= CLASS_HIGH_BIT >> right_shift;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700744 }
745 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700746 // There is no reference offset bitmap. In the non-static case,
747 // walk up the class inheritance hierarchy and find reference
748 // offsets the hard way. In the static case, just consider this
749 // class.
750 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700751 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700752 klass = is_static ? NULL : klass->GetSuperClass()) {
753 size_t num_reference_fields = (is_static
754 ? klass->NumReferenceStaticFields()
755 : klass->NumReferenceInstanceFields());
756 for (size_t i = 0; i < num_reference_fields; ++i) {
757 Field* field = (is_static
758 ? klass->GetStaticField(i)
759 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700760 MemberOffset field_offset = field->GetOffset();
761 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700762 MarkObject(ref);
763 }
764 }
765 }
766}
767
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700768void MarkSweep::CheckReference(const Object* obj, const Object* ref, MemberOffset offset, bool is_static) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700769 const Spaces& spaces = heap_->GetSpaces();
770 // TODO: C++0x auto
771 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
772 if ((*cur)->IsAllocSpace() && (*cur)->Contains(ref)) {
773 DCHECK(IsMarked(obj));
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700774
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700775 bool is_marked = IsMarked(ref);
776 if (!is_marked) {
777 LOG(INFO) << **cur;
778 LOG(WARNING) << (is_static ? "Static ref'" : "Instance ref'") << PrettyTypeOf(ref)
779 << "' (" << reinterpret_cast<const void*>(ref) << ") in '" << PrettyTypeOf(obj)
780 << "' (" << reinterpret_cast<const void*>(obj) << ") at offset "
781 << reinterpret_cast<void*>(offset.Int32Value()) << " wasn't marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700782
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700783 const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
784 DCHECK(klass != NULL);
785 const ObjectArray<Field>* fields = is_static ? klass->GetSFields() : klass->GetIFields();
786 DCHECK(fields != NULL);
787 bool found = false;
788 for (int32_t i = 0; i < fields->GetLength(); ++i) {
789 const Field* cur = fields->Get(i);
790 if (cur->GetOffset().Int32Value() == offset.Int32Value()) {
791 LOG(WARNING) << "Field referencing the alloc space was " << PrettyField(cur);
792 found = true;
793 break;
794 }
795 }
796 if (!found) {
797 LOG(WARNING) << "Could not find field in object alloc space with offset " << offset.Int32Value();
798 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700799
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700800 bool obj_marked = heap_->GetCardTable()->IsDirty(obj);
801 if (!obj_marked) {
802 LOG(WARNING) << "Object '" << PrettyTypeOf(obj) << "' "
803 << "(" << reinterpret_cast<const void*>(obj) << ") contains references to "
804 << "the alloc space, but wasn't card marked";
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700805 }
806 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700807 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700808 break;
Ian Rogers5d76c432011-10-31 21:42:49 -0700809 }
810}
811
Carl Shapiro69759ea2011-07-21 18:13:35 -0700812// Scans the header, static field references, and interface pointers
813// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700814inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700815#ifndef NDEBUG
816 ++class_count_;
817#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700818 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700819 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700820}
821
822// Scans the header of all array objects. If the array object is
823// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700824inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700825#ifndef NDEBUG
826 ++array_count_;
827#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700828 MarkObject(obj->GetClass());
829 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700830 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700831 for (int32_t i = 0; i < array->GetLength(); ++i) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700832 const Object* element = array->GetWithoutChecks(i);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700833 MarkObject(element);
834 }
835 }
836}
837
Carl Shapiro69759ea2011-07-21 18:13:35 -0700838// Process the "referent" field in a java.lang.ref.Reference. If the
839// referent has not yet been marked, put it on the appropriate list in
840// the gcHeap for later processing.
841void MarkSweep::DelayReferenceReferent(Object* obj) {
842 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700843 Class* klass = obj->GetClass();
844 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700845 DCHECK(klass->IsReferenceClass());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800846 Object* pending = obj->GetFieldObject<Object*>(heap_->GetReferencePendingNextOffset(), false);
847 Object* referent = heap_->GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700848 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700849 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700850 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700851 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700852 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700853 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700854 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700855 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700856 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700857 list = &phantom_reference_list_;
858 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700859 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800860 heap_->EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700861 }
862}
863
864// Scans the header and field references of a data object. If the
865// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700866// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700867inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700868#ifndef NDEBUG
869 ++other_count_;
870#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700871 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700872 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700873 DelayReferenceReferent(const_cast<Object*>(obj));
874 }
875}
876
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700877void MarkSweep::ScanRoot(const Object* obj) {
878 ScanObject(obj);
879}
880
Carl Shapiro69759ea2011-07-21 18:13:35 -0700881// Scans an object reference. Determines the type of the reference
882// and dispatches to a specialized scanning routine.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700883void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700884 DCHECK(obj != NULL);
885 DCHECK(obj->GetClass() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700886#ifndef NDEBUG
887 if (!IsMarked(obj)) {
888 heap_->DumpSpaces();
889 LOG(FATAL) << "Scanning unmarked object " << reinterpret_cast<const void*>(obj);
890 }
891#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700892 if (obj->IsClass()) {
893 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700894 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700895 ScanArray(obj);
896 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700897 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700898 }
899}
900
Ian Rogers5d76c432011-10-31 21:42:49 -0700901// Scan anything that's on the mark stack.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700902void MarkSweep::ProcessMarkStack() {
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700903 if (kUseMarkStackPrefetch) {
904 const size_t fifo_size = 4;
905 const size_t fifo_mask = fifo_size - 1;
906 const Object* fifo[fifo_size];
907 for (size_t i = 0;i < fifo_size;++i) {
908 fifo[i] = NULL;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700909 }
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700910 size_t fifo_pos = 0;
911 size_t fifo_count = 0;
912 for (;;) {
913 const Object* obj = fifo[fifo_pos & fifo_mask];
914 if (obj != NULL) {
915 ScanObject(obj);
916 fifo[fifo_pos & fifo_mask] = NULL;
917 --fifo_count;
918 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700919
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700920 if (!mark_stack_->IsEmpty()) {
921 const Object* obj = mark_stack_->PopBack();
922 DCHECK(obj != NULL);
923 fifo[fifo_pos & fifo_mask] = obj;
924 __builtin_prefetch(obj);
925 fifo_count++;
926 }
927 fifo_pos++;
928
929 if (!fifo_count) {
930 CHECK(mark_stack_->IsEmpty()) << mark_stack_->Size();
931 break;
932 }
933 }
934 } else {
935 while (!mark_stack_->IsEmpty()) {
936 const Object* obj = mark_stack_->PopBack();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700937 DCHECK(obj != NULL);
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700938 ScanObject(obj);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700939 }
940 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700941}
942
Carl Shapiro69759ea2011-07-21 18:13:35 -0700943// Walks the reference list marking any references subject to the
944// reference clearing policy. References with a black referent are
945// removed from the list. References with white referents biased
946// toward saving are blackened and also removed from the list.
947void MarkSweep::PreserveSomeSoftReferences(Object** list) {
948 DCHECK(list != NULL);
949 Object* clear = NULL;
950 size_t counter = 0;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700951
952 DCHECK(mark_stack_->IsEmpty());
953
Carl Shapiro69759ea2011-07-21 18:13:35 -0700954 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800955 Object* ref = heap_->DequeuePendingReference(list);
956 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700957 if (referent == NULL) {
958 // Referent was cleared by the user during marking.
959 continue;
960 }
961 bool is_marked = IsMarked(referent);
962 if (!is_marked && ((++counter) & 1)) {
963 // Referent is white and biased toward saving, mark it.
964 MarkObject(referent);
965 is_marked = true;
966 }
967 if (!is_marked) {
968 // Referent is white, queue it for clearing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800969 heap_->EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700970 }
971 }
972 *list = clear;
973 // Restart the mark with the newly black references added to the
974 // root set.
975 ProcessMarkStack();
976}
977
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700978inline bool MarkSweep::IsMarked(const Object* object) const
979 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
980 if (object >= immune_begin_ && object < immune_end_) {
981 return true;
982 }
983 DCHECK(current_mark_bitmap_ != NULL);
984 if (current_mark_bitmap_->HasAddress(object)) {
985 return current_mark_bitmap_->Test(object);
986 }
987 return heap_->GetMarkBitmap()->Test(object);
988}
989
990
Carl Shapiro69759ea2011-07-21 18:13:35 -0700991// Unlink the reference list clearing references objects with white
992// referents. Cleared references registered to a reference queue are
993// scheduled for appending by the heap worker thread.
994void MarkSweep::ClearWhiteReferences(Object** list) {
995 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700996 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800997 Object* ref = heap_->DequeuePendingReference(list);
998 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700999 if (referent != NULL && !IsMarked(referent)) {
1000 // Referent is white, clear it.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001001 heap_->ClearReferenceReferent(ref);
1002 if (heap_->IsEnqueuable(ref)) {
1003 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001004 }
1005 }
1006 }
1007 DCHECK(*list == NULL);
1008}
1009
1010// Enqueues finalizer references with white referents. White
1011// referents are blackened, moved to the zombie field, and the
1012// referent field is cleared.
1013void MarkSweep::EnqueueFinalizerReferences(Object** list) {
1014 DCHECK(list != NULL);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001015 MemberOffset zombie_offset = heap_->GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001016 bool has_enqueued = false;
1017 while (*list != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001018 Object* ref = heap_->DequeuePendingReference(list);
1019 Object* referent = heap_->GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001020 if (referent != NULL && !IsMarked(referent)) {
1021 MarkObject(referent);
1022 // If the referent is non-null the reference must queuable.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001023 DCHECK(heap_->IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001024 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001025 heap_->ClearReferenceReferent(ref);
1026 heap_->EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001027 has_enqueued = true;
1028 }
1029 }
1030 if (has_enqueued) {
1031 ProcessMarkStack();
1032 }
1033 DCHECK(*list == NULL);
1034}
1035
Carl Shapiro58551df2011-07-24 03:09:51 -07001036// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001037void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
1038 Object** weak_references,
1039 Object** finalizer_references,
1040 Object** phantom_references) {
1041 DCHECK(soft_references != NULL);
1042 DCHECK(weak_references != NULL);
1043 DCHECK(finalizer_references != NULL);
1044 DCHECK(phantom_references != NULL);
1045
1046 // Unless we are in the zygote or required to clear soft references
1047 // with white references, preserve some white referents.
Ian Rogers2945e242012-06-03 14:45:16 -07001048 if (!clear_soft && !Runtime::Current()->IsZygote()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001049 PreserveSomeSoftReferences(soft_references);
1050 }
1051
1052 // Clear all remaining soft and weak references with white
1053 // referents.
1054 ClearWhiteReferences(soft_references);
1055 ClearWhiteReferences(weak_references);
1056
1057 // Preserve all white objects with finalize methods and schedule
1058 // them for finalization.
1059 EnqueueFinalizerReferences(finalizer_references);
1060
1061 // Clear all f-reachable soft and weak references with white
1062 // referents.
1063 ClearWhiteReferences(soft_references);
1064 ClearWhiteReferences(weak_references);
1065
1066 // Clear all phantom references with white referents.
1067 ClearWhiteReferences(phantom_references);
1068
1069 // At this point all reference lists should be empty.
1070 DCHECK(*soft_references == NULL);
1071 DCHECK(*weak_references == NULL);
1072 DCHECK(*finalizer_references == NULL);
1073 DCHECK(*phantom_references == NULL);
1074}
1075
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001076void MarkSweep::UnBindBitmaps() {
1077 const Spaces& spaces = heap_->GetSpaces();
1078 // TODO: C++0x auto
1079 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
1080 Space* space = *it;
1081 if (space->IsAllocSpace()) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001082 DlMallocSpace* alloc_space = space->AsAllocSpace();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001083 if (alloc_space->temp_bitmap_.get() != NULL) {
1084 // At this point, the temp_bitmap holds our old mark bitmap.
1085 SpaceBitmap* new_bitmap = alloc_space->temp_bitmap_.release();
1086 GetHeap()->GetMarkBitmap()->ReplaceBitmap(alloc_space->mark_bitmap_.get(), new_bitmap);
1087 CHECK_EQ(alloc_space->mark_bitmap_.release(), alloc_space->live_bitmap_.get());
1088 alloc_space->mark_bitmap_.reset(new_bitmap);
1089 DCHECK(alloc_space->temp_bitmap_.get() == NULL);
1090 }
1091 }
1092 }
1093}
1094
Carl Shapiro69759ea2011-07-21 18:13:35 -07001095MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -07001096#ifndef NDEBUG
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001097 VLOG(heap) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -07001098#endif
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001099 // Ensure that the mark stack is empty.
1100 CHECK(mark_stack_->IsEmpty());
1101
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001102 // Clear all of the alloc spaces' mark bitmaps.
1103 const Spaces& spaces = heap_->GetSpaces();
1104 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001105 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -07001106 ContinuousSpace* space = *it;
1107 if (space->GetGcRetentionPolicy() != kGcRetentionPolicyNeverCollect) {
1108 space->GetMarkBitmap()->Clear();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001109 }
1110 }
Mathieu Chartier5301cd22012-05-31 12:11:36 -07001111 mark_stack_->Reset();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001112
1113 // Reset the marked large objects.
1114 LargeObjectSpace* large_objects = GetHeap()->GetLargeObjectsSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001115 large_objects->GetMarkObjects()->Clear();
Carl Shapiro69759ea2011-07-21 18:13:35 -07001116}
1117
1118} // namespace art