blob: 6b565a130e6ecba2be6ca6599c334ceb2d3ff5b8 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro69759ea2011-07-21 18:13:35 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "mark_sweep.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07004
Carl Shapiro58551df2011-07-24 03:09:51 -07005#include <climits>
6#include <vector>
7
Elliott Hughes410c0c82011-09-01 17:58:25 -07008#include "class_loader.h"
Brian Carlstrom693267a2011-09-06 09:25:34 -07009#include "dex_cache.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070010#include "heap.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070011#include "indirect_reference_table.h"
12#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "logging.h"
14#include "macros.h"
15#include "mark_stack.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070016#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070018#include "runtime.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070019#include "space.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070020#include "timing_logger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021#include "thread.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070022
Carl Shapiro69759ea2011-07-21 18:13:35 -070023namespace art {
24
Carl Shapiro58551df2011-07-24 03:09:51 -070025bool MarkSweep::Init() {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070026 mark_stack_ = MarkStack::Create();
Carl Shapiro58551df2011-07-24 03:09:51 -070027 if (mark_stack_ == NULL) {
28 return false;
29 }
30
31 mark_bitmap_ = Heap::GetMarkBits();
32 live_bitmap_ = Heap::GetLiveBits();
33
34 // TODO: if concurrent, clear the card table.
35
buzbee0d966cf2011-09-08 17:34:58 -070036 // TODO: if concurrent, enable card marking in compiler
37
Carl Shapiro58551df2011-07-24 03:09:51 -070038 // TODO: check that the mark bitmap is entirely clear.
39
40 return true;
41}
42
Elliott Hughesb0663112011-10-19 18:16:37 -070043inline void MarkSweep::MarkObject0(const Object* obj, bool check_finger) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070044 DCHECK(obj != NULL);
45 if (obj < condemned_) {
46 DCHECK(IsMarked(obj));
47 return;
48 }
49 bool is_marked = mark_bitmap_->Test(obj);
50 // This object was not previously marked.
51 if (!is_marked) {
52 mark_bitmap_->Set(obj);
53 if (check_finger && obj < finger_) {
54 // The object must be pushed on to the mark stack.
55 mark_stack_->Push(obj);
56 }
57 }
58}
59
60// Used to mark objects when recursing. Recursion is done by moving
61// the finger across the bitmaps in address order and marking child
62// objects. Any newly-marked objects whose addresses are lower than
63// the finger won't be visited by the bitmap scan, so those objects
64// need to be added to the mark stack.
Elliott Hughesb0663112011-10-19 18:16:37 -070065inline void MarkSweep::MarkObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070066 if (obj != NULL) {
67 MarkObject0(obj, true);
68 }
69}
70
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071void MarkSweep::MarkObjectVisitor(const Object* root, void* arg) {
Brian Carlstrom1f870082011-08-23 16:02:11 -070072 DCHECK(root != NULL);
73 DCHECK(arg != NULL);
74 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
75 mark_sweep->MarkObject0(root, true);
76}
77
Carl Shapiro69759ea2011-07-21 18:13:35 -070078// Marks all objects in the root set.
79void MarkSweep::MarkRoots() {
Brian Carlstrom1f870082011-08-23 16:02:11 -070080 Runtime::Current()->VisitRoots(MarkObjectVisitor, this);
Carl Shapiro69759ea2011-07-21 18:13:35 -070081}
82
Carl Shapiro58551df2011-07-24 03:09:51 -070083void MarkSweep::ScanBitmapCallback(Object* obj, void* finger, void* arg) {
84 MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg);
85 mark_sweep->finger_ = reinterpret_cast<Object*>(finger);
86 mark_sweep->ScanObject(obj);
87}
88
89// Populates the mark stack based on the set of marked objects and
90// recursively marks until the mark stack is emptied.
91void MarkSweep::RecursiveMark() {
Brian Carlstrom1f870082011-08-23 16:02:11 -070092 // RecursiveMark will build the lists of known instances of the Reference classes.
93 // See DelayReferenceReferent for details.
94 CHECK(soft_reference_list_ == NULL);
95 CHECK(weak_reference_list_ == NULL);
96 CHECK(finalizer_reference_list_ == NULL);
97 CHECK(phantom_reference_list_ == NULL);
98 CHECK(cleared_reference_list_ == NULL);
99
Elliott Hughes307f75d2011-10-12 18:04:40 -0700100 TimingLogger timings("MarkSweep::RecursiveMark");
Carl Shapiro58551df2011-07-24 03:09:51 -0700101 void* arg = reinterpret_cast<void*>(this);
102 const std::vector<Space*>& spaces = Heap::GetSpaces();
103 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700104 if (!spaces[i]->IsImageSpace()) {
Carl Shapiro58551df2011-07-24 03:09:51 -0700105 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
Brian Carlstrom1f870082011-08-23 16:02:11 -0700106 mark_bitmap_->ScanWalk(base, &MarkSweep::ScanBitmapCallback, arg);
Carl Shapiro58551df2011-07-24 03:09:51 -0700107 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700108 timings.AddSplit(StringPrintf("ScanWalk space #%i (%s)", i, spaces[i]->GetName().c_str()));
Carl Shapiro58551df2011-07-24 03:09:51 -0700109 }
110 finger_ = reinterpret_cast<Object*>(~0);
111 ProcessMarkStack();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700112 timings.AddSplit("ProcessMarkStack");
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700113 if (Heap::IsVerboseHeap()) {
114 timings.Dump();
115 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700116}
117
118void MarkSweep::ReMarkRoots() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700119 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700120}
121
Elliott Hughes410c0c82011-09-01 17:58:25 -0700122void MarkSweep::SweepJniWeakGlobals() {
123 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
124 MutexLock mu(vm->weak_globals_lock);
125 IndirectReferenceTable* table = &vm->weak_globals;
126 typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
127 for (It it = table->begin(), end = table->end(); it != end; ++it) {
128 const Object** entry = *it;
129 if (!IsMarked(*entry)) {
130 *entry = kClearedJniWeakGlobal;
131 }
132 }
133}
134
Elliott Hughes410c0c82011-09-01 17:58:25 -0700135void MarkSweep::SweepSystemWeaks() {
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700136 Runtime::Current()->GetInternTable()->SweepInternTableWeaks(IsMarked, this);
137 Runtime::Current()->GetMonitorList()->SweepMonitorList(IsMarked, this);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700138 SweepJniWeakGlobals();
Carl Shapiro58551df2011-07-24 03:09:51 -0700139}
140
Brian Carlstrom78128a62011-09-15 17:21:19 -0700141void MarkSweep::SweepCallback(size_t num_ptrs, void** ptrs, void* arg) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700142 // TODO: lock heap if concurrent
Elliott Hughes307f75d2011-10-12 18:04:40 -0700143 size_t freed_objects = num_ptrs;
144 size_t freed_bytes = 0;
Carl Shapiro58551df2011-07-24 03:09:51 -0700145 Space* space = static_cast<Space*>(arg);
146 for (size_t i = 0; i < num_ptrs; ++i) {
147 Object* obj = static_cast<Object*>(ptrs[i]);
Elliott Hughes307f75d2011-10-12 18:04:40 -0700148 freed_bytes += space->AllocationSize(obj);
Elliott Hughes1e200942011-10-12 18:59:24 -0700149 Heap::GetLiveBits()->Clear(obj);
Carl Shapiro58551df2011-07-24 03:09:51 -0700150 space->Free(obj);
151 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700152 Heap::RecordFreeLocked(freed_objects, freed_bytes);
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700153 // TODO: unlock heap if concurrent
Carl Shapiro58551df2011-07-24 03:09:51 -0700154}
155
156void MarkSweep::Sweep() {
Elliott Hughes2da50362011-10-10 16:57:08 -0700157 SweepSystemWeaks();
158
Carl Shapiro58551df2011-07-24 03:09:51 -0700159 const std::vector<Space*>& spaces = Heap::GetSpaces();
160 for (size_t i = 0; i < spaces.size(); ++i) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700161 if (!spaces[i]->IsImageSpace()) {
Carl Shapiro58551df2011-07-24 03:09:51 -0700162 uintptr_t base = reinterpret_cast<uintptr_t>(spaces[i]->GetBase());
163 uintptr_t limit = reinterpret_cast<uintptr_t>(spaces[i]->GetLimit());
164 void* arg = static_cast<void*>(spaces[i]);
165 HeapBitmap::SweepWalk(*live_bitmap_, *mark_bitmap_, base, limit,
166 &MarkSweep::SweepCallback, arg);
167 }
168 }
169}
170
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171// Scans instance fields.
Elliott Hughesb0663112011-10-19 18:16:37 -0700172inline void MarkSweep::ScanInstanceFields(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700173 DCHECK(obj != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700174 Class* klass = obj->GetClass();
175 DCHECK(klass != NULL);
Elliott Hughes2da50362011-10-10 16:57:08 -0700176 ScanFields(obj, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700177}
178
179// Scans static storage on a Class.
Elliott Hughesb0663112011-10-19 18:16:37 -0700180inline void MarkSweep::ScanStaticFields(const Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700181 DCHECK(klass != NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700182 ScanFields(klass, klass->GetReferenceStaticOffsets(), true);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700183}
184
Elliott Hughesb0663112011-10-19 18:16:37 -0700185inline void MarkSweep::ScanFields(const Object* obj, uint32_t ref_offsets, bool is_static) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700186 if (ref_offsets != CLASS_WALK_SUPER) {
187 // Found a reference offset bitmap. Mark the specified offsets.
188 while (ref_offsets != 0) {
189 size_t right_shift = CLZ(ref_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
191 const Object* ref = obj->GetFieldObject<const Object*>(byte_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700192 MarkObject(ref);
193 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
194 }
195 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700196 // There is no reference offset bitmap. In the non-static case,
197 // walk up the class inheritance hierarchy and find reference
198 // offsets the hard way. In the static case, just consider this
199 // class.
200 for (const Class* klass = is_static ? obj->AsClass() : obj->GetClass();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700201 klass != NULL;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700202 klass = is_static ? NULL : klass->GetSuperClass()) {
203 size_t num_reference_fields = (is_static
204 ? klass->NumReferenceStaticFields()
205 : klass->NumReferenceInstanceFields());
206 for (size_t i = 0; i < num_reference_fields; ++i) {
207 Field* field = (is_static
208 ? klass->GetStaticField(i)
209 : klass->GetInstanceField(i));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 MemberOffset field_offset = field->GetOffset();
211 const Object* ref = obj->GetFieldObject<const Object*>(field_offset, false);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212 MarkObject(ref);
213 }
214 }
215 }
216}
217
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218// Scans the header, static field references, and interface pointers
219// of a class object.
Elliott Hughesb0663112011-10-19 18:16:37 -0700220inline void MarkSweep::ScanClass(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700221#ifndef NDEBUG
222 ++class_count_;
223#endif
Brian Carlstrom693267a2011-09-06 09:25:34 -0700224 ScanInstanceFields(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700225 ScanStaticFields(obj->AsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700226}
227
228// Scans the header of all array objects. If the array object is
229// specialized to a reference type, scans the array data as well.
Elliott Hughesb0663112011-10-19 18:16:37 -0700230inline void MarkSweep::ScanArray(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700231#ifndef NDEBUG
232 ++array_count_;
233#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700234 MarkObject(obj->GetClass());
235 if (obj->IsObjectArray()) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700236 const ObjectArray<Object>* array = obj->AsObjectArray<Object>();
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700237 for (int32_t i = 0; i < array->GetLength(); ++i) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700238 const Object* element = array->Get(i);
239 MarkObject(element);
240 }
241 }
242}
243
Carl Shapiro69759ea2011-07-21 18:13:35 -0700244// Process the "referent" field in a java.lang.ref.Reference. If the
245// referent has not yet been marked, put it on the appropriate list in
246// the gcHeap for later processing.
247void MarkSweep::DelayReferenceReferent(Object* obj) {
248 DCHECK(obj != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700249 Class* klass = obj->GetClass();
250 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700251 DCHECK(klass->IsReferenceClass());
252 Object* pending = obj->GetFieldObject<Object*>(Heap::GetReferencePendingNextOffset(), false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700253 Object* referent = Heap::GetReferenceReferent(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700254 if (pending == NULL && referent != NULL && !IsMarked(referent)) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700255 Object** list = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700256 if (klass->IsSoftReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257 list = &soft_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700258 } else if (klass->IsWeakReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700259 list = &weak_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700260 } else if (klass->IsFinalizerReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700261 list = &finalizer_reference_list_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700262 } else if (klass->IsPhantomReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700263 list = &phantom_reference_list_;
264 }
Brian Carlstrom0796af02011-10-12 14:31:45 -0700265 DCHECK(list != NULL) << PrettyClass(klass) << " " << std::hex << klass->GetAccessFlags();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700266 Heap::EnqueuePendingReference(obj, list);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700267 }
268}
269
270// Scans the header and field references of a data object. If the
271// scanned object is a reference subclass, it is scheduled for later
Elliott Hughesadb460d2011-10-05 17:02:34 -0700272// processing.
Elliott Hughesb0663112011-10-19 18:16:37 -0700273inline void MarkSweep::ScanOther(const Object* obj) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700274#ifndef NDEBUG
275 ++other_count_;
276#endif
Carl Shapiro69759ea2011-07-21 18:13:35 -0700277 ScanInstanceFields(obj);
Elliott Hughes352a4242011-10-31 15:15:21 -0700278 if (obj->GetClass()->IsReferenceClass()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700279 DelayReferenceReferent(const_cast<Object*>(obj));
280 }
281}
282
283// Scans an object reference. Determines the type of the reference
284// and dispatches to a specialized scanning routine.
Elliott Hughesb0663112011-10-19 18:16:37 -0700285inline void MarkSweep::ScanObject(const Object* obj) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700286 DCHECK(obj != NULL);
287 DCHECK(obj->GetClass() != NULL);
288 DCHECK(IsMarked(obj));
289 if (obj->IsClass()) {
290 ScanClass(obj);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700291 } else if (obj->IsArrayInstance()) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700292 ScanArray(obj);
293 } else {
Carl Shapiro58551df2011-07-24 03:09:51 -0700294 ScanOther(obj);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700295 }
296}
297
298// Scan anything that's on the mark stack. We can't use the bitmaps
299// anymore, so use a finger that points past the end of them.
300void MarkSweep::ProcessMarkStack() {
301 while (!mark_stack_->IsEmpty()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700302 const Object* obj = mark_stack_->Pop();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700303 ScanObject(obj);
304 }
305}
306
307void MarkSweep::ScanDirtyObjects() {
308 ProcessMarkStack();
309}
310
Carl Shapiro69759ea2011-07-21 18:13:35 -0700311// Walks the reference list marking any references subject to the
312// reference clearing policy. References with a black referent are
313// removed from the list. References with white referents biased
314// toward saving are blackened and also removed from the list.
315void MarkSweep::PreserveSomeSoftReferences(Object** list) {
316 DCHECK(list != NULL);
317 Object* clear = NULL;
318 size_t counter = 0;
319 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700320 Object* ref = Heap::DequeuePendingReference(list);
321 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700322 if (referent == NULL) {
323 // Referent was cleared by the user during marking.
324 continue;
325 }
326 bool is_marked = IsMarked(referent);
327 if (!is_marked && ((++counter) & 1)) {
328 // Referent is white and biased toward saving, mark it.
329 MarkObject(referent);
330 is_marked = true;
331 }
332 if (!is_marked) {
333 // Referent is white, queue it for clearing.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700334 Heap::EnqueuePendingReference(ref, &clear);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700335 }
336 }
337 *list = clear;
338 // Restart the mark with the newly black references added to the
339 // root set.
340 ProcessMarkStack();
341}
342
343// Unlink the reference list clearing references objects with white
344// referents. Cleared references registered to a reference queue are
345// scheduled for appending by the heap worker thread.
346void MarkSweep::ClearWhiteReferences(Object** list) {
347 DCHECK(list != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700348 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700349 Object* ref = Heap::DequeuePendingReference(list);
350 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700351 if (referent != NULL && !IsMarked(referent)) {
352 // Referent is white, clear it.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700353 Heap::ClearReferenceReferent(ref);
354 if (Heap::IsEnqueuable(ref)) {
355 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700356 }
357 }
358 }
359 DCHECK(*list == NULL);
360}
361
362// Enqueues finalizer references with white referents. White
363// referents are blackened, moved to the zombie field, and the
364// referent field is cleared.
365void MarkSweep::EnqueueFinalizerReferences(Object** list) {
366 DCHECK(list != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700367 MemberOffset zombie_offset = Heap::GetFinalizerReferenceZombieOffset();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700368 bool has_enqueued = false;
369 while (*list != NULL) {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700370 Object* ref = Heap::DequeuePendingReference(list);
371 Object* referent = Heap::GetReferenceReferent(ref);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700372 if (referent != NULL && !IsMarked(referent)) {
373 MarkObject(referent);
374 // If the referent is non-null the reference must queuable.
Elliott Hughesadb460d2011-10-05 17:02:34 -0700375 DCHECK(Heap::IsEnqueuable(ref));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376 ref->SetFieldObject(zombie_offset, referent, false);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700377 Heap::ClearReferenceReferent(ref);
378 Heap::EnqueueReference(ref, &cleared_reference_list_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700379 has_enqueued = true;
380 }
381 }
382 if (has_enqueued) {
383 ProcessMarkStack();
384 }
385 DCHECK(*list == NULL);
386}
387
Carl Shapiro58551df2011-07-24 03:09:51 -0700388// Process reference class instances and schedule finalizations.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700389void MarkSweep::ProcessReferences(Object** soft_references, bool clear_soft,
390 Object** weak_references,
391 Object** finalizer_references,
392 Object** phantom_references) {
393 DCHECK(soft_references != NULL);
394 DCHECK(weak_references != NULL);
395 DCHECK(finalizer_references != NULL);
396 DCHECK(phantom_references != NULL);
397
398 // Unless we are in the zygote or required to clear soft references
399 // with white references, preserve some white referents.
400 if (clear_soft) {
401 PreserveSomeSoftReferences(soft_references);
402 }
403
404 // Clear all remaining soft and weak references with white
405 // referents.
406 ClearWhiteReferences(soft_references);
407 ClearWhiteReferences(weak_references);
408
409 // Preserve all white objects with finalize methods and schedule
410 // them for finalization.
411 EnqueueFinalizerReferences(finalizer_references);
412
413 // Clear all f-reachable soft and weak references with white
414 // referents.
415 ClearWhiteReferences(soft_references);
416 ClearWhiteReferences(weak_references);
417
418 // Clear all phantom references with white referents.
419 ClearWhiteReferences(phantom_references);
420
421 // At this point all reference lists should be empty.
422 DCHECK(*soft_references == NULL);
423 DCHECK(*weak_references == NULL);
424 DCHECK(*finalizer_references == NULL);
425 DCHECK(*phantom_references == NULL);
426}
427
Carl Shapiro69759ea2011-07-21 18:13:35 -0700428MarkSweep::~MarkSweep() {
Elliott Hughes352a4242011-10-31 15:15:21 -0700429#ifndef NDEBUG
430 if (Heap::IsVerboseHeap()) {
431 LOG(INFO) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ << " other=" << other_count_;
432 }
433#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700434 delete mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700435 mark_bitmap_->Clear();
436}
437
438} // namespace art