2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "alloc/CardTable.h"
19 #include "alloc/HeapBitmap.h"
20 #include "alloc/HeapBitmapInlines.h"
21 #include "alloc/HeapInternal.h"
22 #include "alloc/HeapSource.h"
23 #include "alloc/MarkSweep.h"
24 #include "alloc/Visit.h"
25 #include "alloc/VisitInlines.h"
26 #include <limits.h> // for ULONG_MAX
27 #include <sys/mman.h> // for madvise(), mmap()
30 typedef unsigned long Word;
31 const size_t kWordSize = sizeof(Word);
34 * Returns true if the given object is marked.
36 static bool isMarked(const Object *obj, const GcMarkContext *ctx)
38 return dvmHeapBitmapIsObjectBitSet(ctx->bitmap, obj);
42 * Initializes the stack top and advises the mark stack pages as needed.
44 static bool createMarkStack(GcMarkStack *stack)
46 assert(stack != NULL);
47 size_t length = dvmHeapSourceGetIdealFootprint() * sizeof(Object*) /
48 (sizeof(Object) + HEAP_SOURCE_CHUNK_OVERHEAD);
49 madvise(stack->base, length, MADV_NORMAL);
50 stack->top = stack->base;
55 * Assigns NULL to the stack top and advises the mark stack pages as
58 static void destroyMarkStack(GcMarkStack *stack)
60 assert(stack != NULL);
61 madvise(stack->base, stack->length, MADV_DONTNEED);
66 * Pops an object from the mark stack.
68 static void markStackPush(GcMarkStack *stack, const Object *obj)
70 assert(stack != NULL);
71 assert(stack->base <= stack->top);
72 assert(stack->limit > stack->top);
79 * Pushes an object on the mark stack.
81 static const Object *markStackPop(GcMarkStack *stack)
83 assert(stack != NULL);
84 assert(stack->base < stack->top);
85 assert(stack->limit > stack->top);
90 bool dvmHeapBeginMarkStep(bool isPartial)
92 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
94 if (!createMarkStack(&ctx->stack)) {
98 ctx->immuneLimit = (char*)dvmHeapSourceGetImmuneLimit(isPartial);
102 static long setAndReturnMarkBit(GcMarkContext *ctx, const void *obj)
104 return dvmHeapBitmapSetAndReturnObjectBit(ctx->bitmap, obj);
107 static void markObjectNonNull(const Object *obj, GcMarkContext *ctx,
112 assert(dvmIsValidObject(obj));
114 if (obj < (Object *)ctx->immuneLimit) {
115 assert(isMarked(obj, ctx));
118 if (!setAndReturnMarkBit(ctx, obj)) {
119 /* This object was not previously marked.
121 if (checkFinger && (void *)obj < ctx->finger) {
122 /* This object will need to go on the mark stack.
124 markStackPush(&ctx->stack, obj);
129 /* Used to mark objects when recursing. Recursion is done by moving
130 * the finger across the bitmaps in address order and marking child
131 * objects. Any newly-marked objects whose addresses are lower than
132 * the finger won't be visited by the bitmap scan, so those objects
133 * need to be added to the mark stack.
135 static void markObject(const Object *obj, GcMarkContext *ctx)
138 markObjectNonNull(obj, ctx, true);
143 * Callback applied to root references during the initial root
144 * marking. Marks white objects but does not push them on the mark
147 static void rootMarkObjectVisitor(void *addr, u4 thread, RootType type,
153 assert(addr != NULL);
155 obj = *(Object **)addr;
156 ctx = (GcMarkContext *)arg;
158 markObjectNonNull(obj, ctx, false);
163 * Visits all objects that start on the given card.
165 static void visitCard(Visitor *visitor, u1 *card, void *arg)
167 assert(visitor != NULL);
168 assert(card != NULL);
169 assert(dvmIsValidCard(card));
170 u1 *addr= (u1*)dvmAddrFromCard(card);
171 u1 *limit = addr + GC_CARD_SIZE;
172 for (; addr < limit; addr += HB_OBJECT_ALIGNMENT) {
173 Object *obj = (Object *)addr;
174 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
175 if (isMarked(obj, ctx)) {
176 (*visitor)(obj, arg);
182 * Visits objects on dirty cards marked the mod union table.
184 static void visitModUnionTable(Visitor *visitor, u1 *base, u1 *limit, void *arg)
186 assert(visitor != NULL);
187 assert(base != NULL);
188 assert(limit != NULL);
189 assert(base <= limit);
190 u1 *heapBase = (u1*)dvmHeapSourceGetBase();
191 /* compute the start address in the bit table */
192 assert(base >= heapBase);
193 u4 *bits = (u4*)gDvm.gcHeap->modUnionTableBase;
194 /* compute the end address in the bit table */
195 size_t byteLength = (limit - base) / GC_CARD_SIZE / CHAR_BIT;
196 assert(byteLength <= gDvm.gcHeap->modUnionTableLength);
197 assert(byteLength % sizeof(*bits) == 0);
198 size_t wordLength = byteLength / sizeof(*bits);
199 for (size_t i = 0; i < wordLength; ++i) {
205 for (size_t j = 0; j < sizeof(u4)*CHAR_BIT; ++j) {
206 if (word & (1 << j)) {
207 /* compute the base of the card */
208 size_t offset = (i*sizeof(u4)*CHAR_BIT + j) * GC_CARD_SIZE;
209 u1* addr = heapBase + offset;
210 u1* card = dvmCardFromAddr(addr);
211 /* visit all objects on the card */
212 visitCard(visitor, card, arg);
219 * Visits objects on dirty cards marked in the card table.
221 static void visitCardTable(Visitor *visitor, u1 *base, u1 *limit, void *arg)
223 assert(visitor != NULL);
224 assert(base != NULL);
225 assert(limit != NULL);
226 u1 *start = dvmCardFromAddr(base);
227 u1 *end = dvmCardFromAddr(limit);
228 while (start < end) {
229 u1 *dirty = (u1 *)memchr(start, GC_CARD_DIRTY, end - start);
233 assert(dirty >= start);
234 assert(dirty <= end);
235 assert(dvmIsValidCard(dirty));
236 visitCard(visitor, dirty, arg);
242 Object *threatenBoundary;
244 } ScanImmuneObjectContext;
247 * Marks the referent of an immune object it is threatened.
249 static void scanImmuneObjectReferent(void *addr, void *arg)
251 assert(addr != NULL);
253 Object *obj = *(Object **)addr;
254 ScanImmuneObjectContext *ctx = (ScanImmuneObjectContext *)arg;
258 if (obj >= ctx->threatenBoundary) {
259 /* TODO: set a bit in the mod union table instead. */
260 dvmMarkCard(ctx->currObject);
261 markObjectNonNull(obj, &gDvm.gcHeap->markContext, false);
266 * This function is poorly named, as is its callee.
268 static void scanImmuneObject(void *addr, void *arg)
270 ScanImmuneObjectContext *ctx = (ScanImmuneObjectContext *)arg;
271 Object *obj = (Object *)addr;
272 ctx->currObject = obj;
273 visitObject(scanImmuneObjectReferent, obj, arg);
277 * Verifies that immune objects have their referents marked.
279 static void verifyImmuneObjectsVisitor(void *addr, void *arg)
281 assert(addr != NULL);
283 Object *obj = *(Object **)addr;
284 GcMarkContext *ctx = (GcMarkContext *)arg;
285 if (obj == NULL || obj < (Object *)ctx->immuneLimit) {
288 assert(dvmIsValidObject(obj));
289 if (!isMarked(obj, ctx)) {
290 LOGE("Immune reference %p points to a white threatened object %p",
297 * Visitor that searches for immune objects and verifies that all
298 * threatened referents are marked.
300 static void verifyImmuneObjectsCallback(void *addr, void *arg)
302 assert(addr != NULL);
304 Object *obj = (Object *)addr;
305 GcMarkContext *ctx = (GcMarkContext *)arg;
306 if (obj->clazz == NULL) {
307 LOGI("uninitialized object @ %p (has null clazz pointer)", obj);
310 if (obj < (Object *)ctx->immuneLimit) {
311 visitObject(verifyImmuneObjectsVisitor, obj, ctx);
316 * Verify that immune objects refer to marked objects.
318 static void verifyImmuneObjects()
320 const HeapBitmap *bitmap = dvmHeapSourceGetLiveBits();
321 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
322 dvmHeapBitmapWalk(bitmap, verifyImmuneObjectsCallback, ctx);
325 /* Mark the set of root objects.
327 * Things we need to scan:
328 * - System classes defined by root classloader
330 * - Interpreted stack, from top to "curFrame"
331 * - Dalvik registers (args + local vars)
332 * - JNI local references
333 * - Automatic VM local references (TrackedAlloc)
334 * - Associated Thread/VMThread object
335 * - ThreadGroups (could track & start with these instead of working
336 * upward from Threads)
337 * - Exception currently being thrown, if present
338 * - JNI global references
339 * - Interned string table
340 * - Primitive classes
342 * - gDvm.outOfMemoryObj
343 * - Objects in debugger object registry
346 * - Native stack (for in-progress stuff in the VM)
347 * - The TrackedAlloc stuff watches all native VM references.
351 * Blackens the root set.
353 void dvmHeapMarkRootSet()
355 GcHeap *gcHeap = gDvm.gcHeap;
356 dvmMarkImmuneObjects(gcHeap->markContext.immuneLimit);
357 dvmVisitRoots(rootMarkObjectVisitor, &gcHeap->markContext);
361 * Callback applied to root references during root remarking. Marks
362 * white objects and pushes them on the mark stack.
364 static void rootReMarkObjectVisitor(void *addr, u4 thread, RootType type,
370 assert(addr != NULL);
372 obj = *(Object **)addr;
373 ctx = (GcMarkContext *)arg;
375 markObjectNonNull(obj, ctx, true);
380 * Grays all references in the roots.
382 void dvmHeapReMarkRootSet(void)
384 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
385 assert(ctx->finger == (void *)ULONG_MAX);
386 dvmVisitRoots(rootReMarkObjectVisitor, ctx);
390 * Scans instance fields.
392 static void scanFields(const Object *obj, GcMarkContext *ctx)
395 assert(obj->clazz != NULL);
398 if (obj->clazz->refOffsets != CLASS_WALK_SUPER) {
399 unsigned int refOffsets = obj->clazz->refOffsets;
400 while (refOffsets != 0) {
401 size_t rshift = CLZ(refOffsets);
402 size_t offset = CLASS_OFFSET_FROM_CLZ(rshift);
403 Object *ref = dvmGetFieldObject((Object*)obj, offset);
404 markObject(ref, ctx);
405 refOffsets &= ~(CLASS_HIGH_BIT >> rshift);
408 for (ClassObject *clazz = obj->clazz;
410 clazz = clazz->super) {
411 InstField *field = clazz->ifields;
412 for (int i = 0; i < clazz->ifieldRefCount; ++i, ++field) {
413 void *addr = BYTE_OFFSET((Object *)obj, field->byteOffset);
414 Object *ref = (Object *)((JValue *)addr)->l;
415 markObject(ref, ctx);
422 * Scans the static fields of a class object.
424 static void scanStaticFields(const ClassObject *clazz, GcMarkContext *ctx)
426 assert(clazz != NULL);
428 for (int i = 0; i < clazz->sfieldCount; ++i) {
429 char ch = clazz->sfields[i].field.signature[0];
430 if (ch == '[' || ch == 'L') {
431 Object *obj = (Object *)clazz->sfields[i].value.l;
432 markObject(obj, ctx);
438 * Visit the interfaces of a class object.
440 static void scanInterfaces(const ClassObject *clazz, GcMarkContext *ctx)
442 assert(clazz != NULL);
444 for (int i = 0; i < clazz->interfaceCount; ++i) {
445 markObject((const Object *)clazz->interfaces[i], ctx);
450 * Scans the header, static field references, and interface
451 * pointers of a class object.
453 static void scanClassObject(const Object *obj, GcMarkContext *ctx)
455 const ClassObject *asClass;
458 assert(obj->clazz == gDvm.classJavaLangClass);
460 markObject((const Object *)obj->clazz, ctx);
461 asClass = (const ClassObject *)obj;
462 if (IS_CLASS_FLAG_SET(asClass, CLASS_ISARRAY)) {
463 markObject((const Object *)asClass->elementClass, ctx);
465 /* Do super and the interfaces contain Objects and not dex idx values? */
466 if (asClass->status > CLASS_IDX) {
467 markObject((const Object *)asClass->super, ctx);
469 markObject((const Object *)asClass->classLoader, ctx);
470 scanFields(obj, ctx);
471 scanStaticFields(asClass, ctx);
472 if (asClass->status > CLASS_IDX) {
473 scanInterfaces(asClass, ctx);
478 * Scans the header of all array objects. If the array object is
479 * specialized to a reference type, scans the array data as well.
481 static void scanArrayObject(const Object *obj, GcMarkContext *ctx)
484 assert(obj->clazz != NULL);
486 markObject((const Object *)obj->clazz, ctx);
487 if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISOBJECTARRAY)) {
488 const ArrayObject *array = (const ArrayObject *)obj;
489 const Object **contents = (const Object **)(void *)array->contents;
490 for (size_t i = 0; i < array->length; ++i) {
491 markObject(contents[i], ctx);
497 * Returns class flags relating to Reference subclasses.
499 static int referenceClassFlags(const Object *obj)
501 int flags = CLASS_ISREFERENCE |
502 CLASS_ISWEAKREFERENCE |
503 CLASS_ISFINALIZERREFERENCE |
504 CLASS_ISPHANTOMREFERENCE;
505 return GET_CLASS_FLAG_GROUP(obj->clazz, flags);
509 * Returns true if the object derives from SoftReference.
511 static bool isSoftReference(const Object *obj)
513 return referenceClassFlags(obj) == CLASS_ISREFERENCE;
517 * Returns true if the object derives from WeakReference.
519 static bool isWeakReference(const Object *obj)
521 return referenceClassFlags(obj) & CLASS_ISWEAKREFERENCE;
525 * Returns true if the object derives from FinalizerReference.
527 static bool isFinalizerReference(const Object *obj)
529 return referenceClassFlags(obj) & CLASS_ISFINALIZERREFERENCE;
533 * Returns true if the object derives from PhantomReference.
535 static bool isPhantomReference(const Object *obj)
537 return referenceClassFlags(obj) & CLASS_ISPHANTOMREFERENCE;
541 * Adds a reference to the tail of a circular queue of references.
543 static void enqueuePendingReference(Object *ref, Object **list)
548 assert(list != NULL);
549 offset = gDvm.offJavaLangRefReference_pendingNext;
551 dvmSetFieldObject(ref, offset, ref);
554 Object *head = dvmGetFieldObject(*list, offset);
555 dvmSetFieldObject(ref, offset, head);
556 dvmSetFieldObject(*list, offset, ref);
561 * Removes the reference at the head of a circular queue of
564 static Object *dequeuePendingReference(Object **list)
569 assert(list != NULL);
570 assert(*list != NULL);
571 offset = gDvm.offJavaLangRefReference_pendingNext;
572 head = dvmGetFieldObject(*list, offset);
577 Object *next = dvmGetFieldObject(head, offset);
578 dvmSetFieldObject(*list, offset, next);
581 dvmSetFieldObject(ref, offset, NULL);
586 * Process the "referent" field in a java.lang.ref.Reference. If the
587 * referent has not yet been marked, put it on the appropriate list in
588 * the gcHeap for later processing.
590 static void delayReferenceReferent(Object *obj, GcMarkContext *ctx)
592 GcHeap *gcHeap = gDvm.gcHeap;
593 Object *pending, *referent;
594 size_t pendingNextOffset, referentOffset;
597 assert(obj->clazz != NULL);
598 assert(IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE));
600 pendingNextOffset = gDvm.offJavaLangRefReference_pendingNext;
601 referentOffset = gDvm.offJavaLangRefReference_referent;
602 pending = dvmGetFieldObject(obj, pendingNextOffset);
603 referent = dvmGetFieldObject(obj, referentOffset);
604 if (pending == NULL && referent != NULL && !isMarked(referent, ctx)) {
605 Object **list = NULL;
606 if (isSoftReference(obj)) {
607 list = &gcHeap->softReferences;
608 } else if (isWeakReference(obj)) {
609 list = &gcHeap->weakReferences;
610 } else if (isFinalizerReference(obj)) {
611 list = &gcHeap->finalizerReferences;
612 } else if (isPhantomReference(obj)) {
613 list = &gcHeap->phantomReferences;
615 assert(list != NULL);
616 enqueuePendingReference(obj, list);
621 * Scans the header and field references of a data object.
623 static void scanDataObject(const Object *obj, GcMarkContext *ctx)
626 assert(obj->clazz != NULL);
628 markObject((const Object *)obj->clazz, ctx);
629 scanFields(obj, ctx);
630 if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)) {
631 delayReferenceReferent((Object *)obj, ctx);
636 * Scans an object reference. Determines the type of the reference
637 * and dispatches to a specialized scanning routine.
639 static void scanObject(const Object *obj, GcMarkContext *ctx)
643 assert(isMarked(obj, ctx));
644 assert(obj->clazz != NULL);
645 assert(isMarked(obj, ctx));
646 if (obj->clazz == gDvm.classJavaLangClass) {
647 scanClassObject(obj, ctx);
648 } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) {
649 scanArrayObject(obj, ctx);
651 scanDataObject(obj, ctx);
656 * Scan anything that's on the mark stack. We can't use the bitmaps
657 * anymore, so use a finger that points past the end of them.
659 static void processMarkStack(GcMarkContext *ctx)
664 assert(ctx->finger == (void *)ULONG_MAX);
666 assert(stack->top >= stack->base);
667 while (stack->top > stack->base) {
668 const Object *obj = markStackPop(stack);
669 scanObject(obj, ctx);
674 * Blackens gray objects found on dirty cards.
676 static void scanGrayObjects(GcMarkContext *ctx)
678 HeapBitmap *bitmap = ctx->bitmap;
679 u1 *base = (u1 *)bitmap->base;
680 u1 *limit = (u1 *)ALIGN_UP(bitmap->max, GC_CARD_SIZE);
681 visitCardTable((Visitor *)scanObject, base, limit, ctx);
685 * Iterate through the immune objects and mark their referents. Uses
686 * the mod union table to save scanning time.
688 void dvmHeapScanImmuneObjects(const GcMarkContext *ctx)
690 ScanImmuneObjectContext scanCtx;
691 memset(&scanCtx, 0, sizeof(scanCtx));
692 scanCtx.threatenBoundary = (Object*)ctx->immuneLimit;
693 visitModUnionTable(scanImmuneObject,
694 (u1*)ctx->bitmap->base, (u1*)ctx->immuneLimit,
696 if (gDvm.verifyCardTable) {
697 verifyImmuneObjects();
702 * Callback for scanning each object in the bitmap. The finger is set
703 * to the address corresponding to the lowest address in the next word
704 * of bits in the bitmap.
706 static void scanBitmapCallback(void *addr, void *finger, void *arg)
708 GcMarkContext *ctx = (GcMarkContext *)arg;
709 ctx->finger = (void *)finger;
710 scanObject((Object *)addr, ctx);
713 /* Given bitmaps with the root set marked, find and mark all
714 * reachable objects. When this returns, the entire set of
715 * live objects will be marked and the mark stack will be empty.
717 void dvmHeapScanMarkedObjects(bool isPartial)
719 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
722 assert(ctx->finger == NULL);
725 if (isPartial && dvmHeapSourceGetNumHeaps() > 1) {
726 dvmHeapScanImmuneObjects(ctx);
727 start = (u1 *)ctx->immuneLimit;
729 start = (u1*)ctx->bitmap->base;
732 * All objects reachable from the root set have a bit set in the
733 * mark bitmap. Walk the mark bitmap and blacken these objects.
735 dvmHeapBitmapScanWalk(ctx->bitmap,
736 (uintptr_t)start, ctx->bitmap->max,
740 ctx->finger = (void *)ULONG_MAX;
742 /* Process gray objects until the mark stack it is empty. */
743 processMarkStack(ctx);
746 void dvmHeapReScanMarkedObjects(void)
748 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
751 * The finger must have been set to the maximum value to ensure
752 * that gray objects will be pushed onto the mark stack.
754 assert(ctx->finger == (void *)ULONG_MAX);
755 scanGrayObjects(ctx);
756 processMarkStack(ctx);
760 * Clear the referent field.
762 static void clearReference(Object *reference)
764 size_t offset = gDvm.offJavaLangRefReference_referent;
765 dvmSetFieldObject(reference, offset, NULL);
769 * Returns true if the reference was registered with a reference queue
770 * and has not yet been enqueued.
772 static bool isEnqueuable(const Object *reference)
774 Object *queue = dvmGetFieldObject(reference,
775 gDvm.offJavaLangRefReference_queue);
776 Object *queueNext = dvmGetFieldObject(reference,
777 gDvm.offJavaLangRefReference_queueNext);
778 return queue != NULL && queueNext == NULL;
782 * Schedules a reference to be appended to its reference queue.
784 static void enqueueReference(Object *ref)
787 assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue) != NULL);
788 assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext) == NULL);
789 enqueuePendingReference(ref, &gDvm.gcHeap->clearedReferences);
793 * Walks the reference list marking any references subject to the
794 * reference clearing policy. References with a black referent are
795 * removed from the list. References with white referents biased
796 * toward saving are blackened and also removed from the list.
798 static void preserveSomeSoftReferences(Object **list)
801 Object *ref, *referent;
803 size_t referentOffset;
807 ctx = &gDvm.gcHeap->markContext;
808 referentOffset = gDvm.offJavaLangRefReference_referent;
811 while (*list != NULL) {
812 ref = dequeuePendingReference(list);
813 referent = dvmGetFieldObject(ref, referentOffset);
814 if (referent == NULL) {
815 /* Referent was cleared by the user during marking. */
818 marked = isMarked(referent, ctx);
819 if (!marked && ((++counter) & 1)) {
820 /* Referent is white and biased toward saving, mark it. */
821 markObject(referent, ctx);
825 /* Referent is white, queue it for clearing. */
826 enqueuePendingReference(ref, &clear);
831 * Restart the mark with the newly black references added to the
834 processMarkStack(ctx);
838 * Unlink the reference list clearing references objects with white
839 * referents. Cleared references registered to a reference queue are
840 * scheduled for appending by the heap worker thread.
842 static void clearWhiteReferences(Object **list)
845 Object *ref, *referent;
846 size_t referentOffset;
848 ctx = &gDvm.gcHeap->markContext;
849 referentOffset = gDvm.offJavaLangRefReference_referent;
850 while (*list != NULL) {
851 ref = dequeuePendingReference(list);
852 referent = dvmGetFieldObject(ref, referentOffset);
853 if (referent != NULL && !isMarked(referent, ctx)) {
854 /* Referent is white, clear it. */
856 if (isEnqueuable(ref)) {
857 enqueueReference(ref);
861 assert(*list == NULL);
865 * Enqueues finalizer references with white referents. White
866 * referents are blackened, moved to the zombie field, and the
867 * referent field is cleared.
869 static void enqueueFinalizerReferences(Object **list)
871 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
872 size_t referentOffset = gDvm.offJavaLangRefReference_referent;
873 size_t zombieOffset = gDvm.offJavaLangRefFinalizerReference_zombie;
874 bool hasEnqueued = false;
875 while (*list != NULL) {
876 Object *ref = dequeuePendingReference(list);
877 Object *referent = dvmGetFieldObject(ref, referentOffset);
878 if (referent != NULL && !isMarked(referent, ctx)) {
879 markObject(referent, ctx);
880 /* If the referent is non-null the reference must queuable. */
881 assert(isEnqueuable(ref));
882 dvmSetFieldObject(ref, zombieOffset, referent);
884 enqueueReference(ref);
889 processMarkStack(ctx);
891 assert(*list == NULL);
895 * This object is an instance of a class that overrides finalize(). Mark
898 * This is called when Object.<init> completes normally. It's also
899 * called for clones of finalizable objects.
901 void dvmSetFinalizable(Object *obj)
903 Thread *self = dvmThreadSelf();
904 assert(self != NULL);
905 Method *meth = gDvm.methJavaLangRefFinalizerReferenceAdd;
906 assert(meth != NULL);
908 dvmCallMethod(self, meth, NULL, &unusedResult, obj);
912 * Process reference class instances and schedule finalizations.
914 void dvmHeapProcessReferences(Object **softReferences, bool clearSoftRefs,
915 Object **weakReferences,
916 Object **finalizerReferences,
917 Object **phantomReferences)
919 assert(softReferences != NULL);
920 assert(weakReferences != NULL);
921 assert(finalizerReferences != NULL);
922 assert(phantomReferences != NULL);
924 * Unless we are in the zygote or required to clear soft
925 * references with white references, preserve some white
928 if (!gDvm.zygote && !clearSoftRefs) {
929 preserveSomeSoftReferences(softReferences);
932 * Clear all remaining soft and weak references with white
935 clearWhiteReferences(softReferences);
936 clearWhiteReferences(weakReferences);
938 * Preserve all white objects with finalize methods and schedule
939 * them for finalization.
941 enqueueFinalizerReferences(finalizerReferences);
943 * Clear all f-reachable soft and weak references with white
946 clearWhiteReferences(softReferences);
947 clearWhiteReferences(weakReferences);
949 * Clear all phantom references with white referents.
951 clearWhiteReferences(phantomReferences);
953 * At this point all reference lists should be empty.
955 assert(*softReferences == NULL);
956 assert(*weakReferences == NULL);
957 assert(*finalizerReferences == NULL);
958 assert(*phantomReferences == NULL);
962 * Pushes a list of cleared references out to the managed heap.
964 void dvmEnqueueClearedReferences(Object **cleared)
966 assert(cleared != NULL);
967 if (*cleared != NULL) {
968 Thread *self = dvmThreadSelf();
969 assert(self != NULL);
970 Method *meth = gDvm.methJavaLangRefReferenceQueueAdd;
971 assert(meth != NULL);
973 Object *reference = *cleared;
974 dvmCallMethod(self, meth, NULL, &unused, reference);
979 void dvmHeapFinishMarkStep()
983 ctx = &gDvm.gcHeap->markContext;
985 /* The mark bits are now not needed.
987 dvmHeapSourceZeroMarkBitmap();
989 /* Clean up everything else associated with the marking process.
991 destroyMarkStack(&ctx->stack);
1002 static void sweepBitmapCallback(size_t numPtrs, void **ptrs, void *arg)
1004 SweepContext *ctx = (SweepContext *)arg;
1006 if (ctx->isConcurrent) {
1009 ctx->numBytes += dvmHeapSourceFreeList(numPtrs, ptrs);
1010 ctx->numObjects += numPtrs;
1011 if (ctx->isConcurrent) {
1017 * Returns true if the given object is unmarked. This assumes that
1018 * the bitmaps have not yet been swapped.
1020 static int isUnmarkedObject(void *obj)
1022 return !isMarked((Object *)obj, &gDvm.gcHeap->markContext);
1025 void sweepWeakJniGlobals(void)
1027 IndirectRefTable *table = &gDvm.jniWeakGlobalRefTable;
1028 Object **entry = table->table;
1029 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
1030 int numEntries = dvmIndirectRefTableEntries(table);
1031 for (int i = 0; i < numEntries; ++i) {
1032 if (entry[i] != NULL && !isMarked(entry[i], ctx)) {
1039 * Process all the internal system structures that behave like
1040 * weakly-held objects.
1042 void dvmHeapSweepSystemWeaks(void)
1044 dvmGcDetachDeadInternedStrings(isUnmarkedObject);
1045 dvmSweepMonitorList(&gDvm.monitorList, isUnmarkedObject);
1046 sweepWeakJniGlobals();
1050 * Walk through the list of objects that haven't been marked and free
1051 * them. Assumes the bitmaps have been swapped.
1053 void dvmHeapSweepUnmarkedObjects(bool isPartial, bool isConcurrent,
1054 size_t *numObjects, size_t *numBytes)
1056 uintptr_t base[HEAP_SOURCE_MAX_HEAP_COUNT];
1057 uintptr_t max[HEAP_SOURCE_MAX_HEAP_COUNT];
1059 HeapBitmap *prevLive, *prevMark;
1060 size_t numHeaps, numSweepHeaps;
1062 numHeaps = dvmHeapSourceGetNumHeaps();
1063 dvmHeapSourceGetRegions(base, max, NULL, numHeaps);
1065 assert((uintptr_t)gDvm.gcHeap->markContext.immuneLimit == base[0]);
1068 numSweepHeaps = numHeaps;
1070 ctx.numObjects = ctx.numBytes = 0;
1071 ctx.isConcurrent = isConcurrent;
1072 prevLive = dvmHeapSourceGetMarkBits();
1073 prevMark = dvmHeapSourceGetLiveBits();
1074 for (size_t i = 0; i < numSweepHeaps; ++i) {
1075 dvmHeapBitmapSweepWalk(prevLive, prevMark, base[i], max[i],
1076 sweepBitmapCallback, &ctx);
1078 *numObjects = ctx.numObjects;
1079 *numBytes = ctx.numBytes;
1080 if (gDvm.allocProf.enabled) {
1081 gDvm.allocProf.freeCount += ctx.numObjects;
1082 gDvm.allocProf.freeSize += ctx.numBytes;