| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
| 16 | /* |
| 17 | * Garbage-collecting memory allocator. |
| 18 | */ |
| 19 | #include "Dalvik.h" |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 20 | #include "alloc/HeapBitmap.h" |
| 21 | #include "alloc/Verify.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | #include "alloc/HeapTable.h" |
| 23 | #include "alloc/Heap.h" |
| 24 | #include "alloc/HeapInternal.h" |
| 25 | #include "alloc/DdmHeap.h" |
| 26 | #include "alloc/HeapSource.h" |
| 27 | #include "alloc/MarkSweep.h" |
| 28 | |
| 29 | #include "utils/threads.h" // need Android thread priorities |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 30 | |
| San Mehat | 5a2056c | 2009-09-12 10:10:13 -0700 | [diff] [blame] | 31 | #include <cutils/sched_policy.h> |
| 32 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 33 | #include <sys/time.h> |
| 34 | #include <sys/resource.h> |
| 35 | #include <limits.h> |
| 36 | #include <errno.h> |
| 37 | |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 38 | static const GcSpec kGcForMallocSpec = { |
| Carl Shapiro | 9b6881c | 2011-01-26 18:41:03 -0800 | [diff] [blame] | 39 | true, /* isPartial */ |
| 40 | false, /* isConcurrent */ |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 41 | PRESERVE, |
| 42 | "GC_FOR_ALLOC" |
| Barry Hayes | 1b9b4e4 | 2010-01-04 10:33:46 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 45 | const GcSpec *GC_FOR_MALLOC = &kGcForMallocSpec; |
| 46 | |
| 47 | static const GcSpec kGcConcurrentSpec = { |
| Carl Shapiro | 9b6881c | 2011-01-26 18:41:03 -0800 | [diff] [blame] | 48 | true, /* isPartial */ |
| 49 | true, /* isConcurrent */ |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 50 | PRESERVE, |
| 51 | "GC_CONCURRENT" |
| 52 | }; |
| 53 | |
| 54 | const GcSpec *GC_CONCURRENT = &kGcConcurrentSpec; |
| 55 | |
| 56 | static const GcSpec kGcExplicitSpec = { |
| Carl Shapiro | 9b6881c | 2011-01-26 18:41:03 -0800 | [diff] [blame] | 57 | false, /* isPartial */ |
| 58 | true, /* isConcurrent */ |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 59 | PRESERVE, |
| 60 | "GC_EXPLICIT" |
| 61 | }; |
| 62 | |
| 63 | const GcSpec *GC_EXPLICIT = &kGcExplicitSpec; |
| 64 | |
| 65 | static const GcSpec kGcBeforeOomSpec = { |
| Carl Shapiro | 9b6881c | 2011-01-26 18:41:03 -0800 | [diff] [blame] | 66 | false, /* isPartial */ |
| 67 | false, /* isConcurrent */ |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 68 | CLEAR, |
| 69 | "GC_BEFORE_OOM" |
| 70 | }; |
| 71 | |
| 72 | const GcSpec *GC_BEFORE_OOM = &kGcBeforeOomSpec; |
| 73 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 74 | /* |
| 75 | * Initialize the GC heap. |
| 76 | * |
| 77 | * Returns true if successful, false otherwise. |
| 78 | */ |
| 79 | bool dvmHeapStartup() |
| 80 | { |
| 81 | GcHeap *gcHeap; |
| 82 | |
| Carl Shapiro | df9f08b | 2011-01-18 17:59:30 -0800 | [diff] [blame] | 83 | if (gDvm.heapGrowthLimit == 0) { |
| 84 | gDvm.heapGrowthLimit = gDvm.heapMaximumSize; |
| 85 | } |
| 86 | |
| 87 | gcHeap = dvmHeapSourceStartup(gDvm.heapStartingSize, |
| 88 | gDvm.heapMaximumSize, |
| 89 | gDvm.heapGrowthLimit); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 90 | if (gcHeap == NULL) { |
| 91 | return false; |
| 92 | } |
| 93 | gcHeap->heapWorkerCurrentObject = NULL; |
| 94 | gcHeap->heapWorkerCurrentMethod = NULL; |
| 95 | gcHeap->heapWorkerInterpStartTime = 0LL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 96 | gcHeap->ddmHpifWhen = 0; |
| 97 | gcHeap->ddmHpsgWhen = 0; |
| 98 | gcHeap->ddmHpsgWhat = 0; |
| 99 | gcHeap->ddmNhsgWhen = 0; |
| 100 | gcHeap->ddmNhsgWhat = 0; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 101 | gDvm.gcHeap = gcHeap; |
| 102 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 103 | /* Set up the lists and lock we'll use for finalizable |
| 104 | * and reference objects. |
| 105 | */ |
| 106 | dvmInitMutex(&gDvm.heapWorkerListLock); |
| 107 | gcHeap->finalizableRefs = NULL; |
| 108 | gcHeap->pendingFinalizationRefs = NULL; |
| 109 | gcHeap->referenceOperations = NULL; |
| 110 | |
| Carl Shapiro | df9f08b | 2011-01-18 17:59:30 -0800 | [diff] [blame] | 111 | if (!dvmCardTableStartup(gDvm.heapMaximumSize)) { |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 112 | LOGE_HEAP("card table startup failed."); |
| 113 | return false; |
| 114 | } |
| 115 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 116 | /* Initialize the HeapWorker locks and other state |
| 117 | * that the GC uses. |
| 118 | */ |
| 119 | dvmInitializeHeapWorkerState(); |
| 120 | |
| 121 | return true; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 124 | bool dvmHeapStartupAfterZygote(void) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 125 | { |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 126 | return dvmHeapSourceStartupAfterZygote(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void dvmHeapShutdown() |
| 130 | { |
| 131 | //TODO: make sure we're locked |
| 132 | if (gDvm.gcHeap != NULL) { |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 133 | dvmCardTableShutdown(); |
| 134 | /* Tables are allocated on the native heap; they need to be |
| 135 | * cleaned up explicitly. The process may stick around, so we |
| 136 | * don't want to leak any native memory. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 137 | */ |
| Carl Shapiro | a199eb7 | 2010-02-09 16:26:30 -0800 | [diff] [blame] | 138 | dvmHeapFreeLargeTable(gDvm.gcHeap->finalizableRefs); |
| 139 | gDvm.gcHeap->finalizableRefs = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 140 | |
| Carl Shapiro | a199eb7 | 2010-02-09 16:26:30 -0800 | [diff] [blame] | 141 | dvmHeapFreeLargeTable(gDvm.gcHeap->pendingFinalizationRefs); |
| 142 | gDvm.gcHeap->pendingFinalizationRefs = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 143 | |
| Carl Shapiro | a199eb7 | 2010-02-09 16:26:30 -0800 | [diff] [blame] | 144 | dvmHeapFreeLargeTable(gDvm.gcHeap->referenceOperations); |
| 145 | gDvm.gcHeap->referenceOperations = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 146 | |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 147 | /* Destroy the heap. Any outstanding pointers will point to |
| 148 | * unmapped memory (unless/until someone else maps it). This |
| 149 | * frees gDvm.gcHeap as a side-effect. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 150 | */ |
| Carl Shapiro | a199eb7 | 2010-02-09 16:26:30 -0800 | [diff] [blame] | 151 | dvmHeapSourceShutdown(&gDvm.gcHeap); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | /* |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 156 | * Shutdown any threads internal to the heap. |
| 157 | */ |
| 158 | void dvmHeapThreadShutdown(void) |
| 159 | { |
| 160 | dvmHeapSourceThreadShutdown(); |
| 161 | } |
| 162 | |
| 163 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 164 | * We've been asked to allocate something we can't, e.g. an array so |
| Andy McFadden | 6da743b | 2009-07-15 16:56:00 -0700 | [diff] [blame] | 165 | * large that (length * elementWidth) is larger than 2^31. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 166 | * |
| Andy McFadden | 6da743b | 2009-07-15 16:56:00 -0700 | [diff] [blame] | 167 | * _The Java Programming Language_, 4th edition, says, "you can be sure |
| 168 | * that all SoftReferences to softly reachable objects will be cleared |
| 169 | * before an OutOfMemoryError is thrown." |
| 170 | * |
| 171 | * It's unclear whether that holds for all situations where an OOM can |
| 172 | * be thrown, or just in the context of an allocation that fails due |
| 173 | * to lack of heap space. For simplicity we just throw the exception. |
| 174 | * |
| 175 | * (OOM due to actually running out of space is handled elsewhere.) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 176 | */ |
| 177 | void dvmThrowBadAllocException(const char* msg) |
| 178 | { |
| Andy McFadden | 6da743b | 2009-07-15 16:56:00 -0700 | [diff] [blame] | 179 | dvmThrowException("Ljava/lang/OutOfMemoryError;", msg); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Grab the lock, but put ourselves into THREAD_VMWAIT if it looks like |
| 184 | * we're going to have to wait on the mutex. |
| 185 | */ |
| 186 | bool dvmLockHeap() |
| 187 | { |
| Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 188 | if (dvmTryLockMutex(&gDvm.gcHeapLock) != 0) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 189 | Thread *self; |
| 190 | ThreadStatus oldStatus; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 191 | |
| 192 | self = dvmThreadSelf(); |
| Carl Shapiro | 5617ad3 | 2010-07-02 10:50:57 -0700 | [diff] [blame] | 193 | oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); |
| Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 194 | dvmLockMutex(&gDvm.gcHeapLock); |
| Carl Shapiro | 5617ad3 | 2010-07-02 10:50:57 -0700 | [diff] [blame] | 195 | dvmChangeStatus(self, oldStatus); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | void dvmUnlockHeap() |
| 202 | { |
| 203 | dvmUnlockMutex(&gDvm.gcHeapLock); |
| 204 | } |
| 205 | |
| 206 | /* Pop an object from the list of pending finalizations and |
| 207 | * reference clears/enqueues, and return the object. |
| 208 | * The caller must call dvmReleaseTrackedAlloc() |
| 209 | * on the object when finished. |
| 210 | * |
| 211 | * Typically only called by the heap worker thread. |
| 212 | */ |
| 213 | Object *dvmGetNextHeapWorkerObject(HeapWorkerOperation *op) |
| 214 | { |
| 215 | Object *obj; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 216 | GcHeap *gcHeap = gDvm.gcHeap; |
| 217 | |
| 218 | assert(op != NULL); |
| 219 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 220 | dvmLockMutex(&gDvm.heapWorkerListLock); |
| 221 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 222 | obj = dvmHeapGetNextObjectFromLargeTable(&gcHeap->referenceOperations); |
| 223 | if (obj != NULL) { |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 224 | *op = WORKER_ENQUEUE; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 225 | } else { |
| 226 | obj = dvmHeapGetNextObjectFromLargeTable( |
| 227 | &gcHeap->pendingFinalizationRefs); |
| 228 | if (obj != NULL) { |
| 229 | *op = WORKER_FINALIZE; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (obj != NULL) { |
| 234 | /* Don't let the GC collect the object until the |
| 235 | * worker thread is done with it. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 236 | */ |
| 237 | dvmAddTrackedAlloc(obj, NULL); |
| 238 | } |
| 239 | |
| 240 | dvmUnlockMutex(&gDvm.heapWorkerListLock); |
| 241 | |
| 242 | return obj; |
| 243 | } |
| 244 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 245 | /* Do a full garbage collection, which may grow the |
| 246 | * heap as a side-effect if the live set is large. |
| 247 | */ |
| Carl Shapiro | a371fad | 2011-01-23 15:58:31 -0800 | [diff] [blame] | 248 | static void gcForMalloc(bool clearSoftReferences) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 249 | { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 250 | if (gDvm.allocProf.enabled) { |
| 251 | Thread* self = dvmThreadSelf(); |
| 252 | gDvm.allocProf.gcCount++; |
| 253 | if (self != NULL) { |
| 254 | self->allocProf.gcCount++; |
| 255 | } |
| 256 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 257 | /* This may adjust the soft limit as a side-effect. |
| 258 | */ |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 259 | const GcSpec *spec = clearSoftReferences ? GC_BEFORE_OOM : GC_FOR_MALLOC; |
| 260 | dvmCollectGarbageInternal(spec); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* Try as hard as possible to allocate some memory. |
| 264 | */ |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 265 | static void *tryMalloc(size_t size) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 266 | { |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 267 | void *ptr; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 268 | |
| 269 | /* Don't try too hard if there's no way the allocation is |
| 270 | * going to succeed. We have to collect SoftReferences before |
| 271 | * throwing an OOME, though. |
| 272 | */ |
| Carl Shapiro | df9f08b | 2011-01-18 17:59:30 -0800 | [diff] [blame] | 273 | if (size >= gDvm.heapGrowthLimit) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 274 | LOGW_HEAP("dvmMalloc(%zu/0x%08zx): " |
| 275 | "someone's allocating a huge buffer\n", size, size); |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 276 | ptr = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 277 | goto collect_soft_refs; |
| 278 | } |
| 279 | |
| 280 | //TODO: figure out better heuristics |
| 281 | // There will be a lot of churn if someone allocates a bunch of |
| 282 | // big objects in a row, and we hit the frag case each time. |
| 283 | // A full GC for each. |
| 284 | // Maybe we grow the heap in bigger leaps |
| 285 | // Maybe we skip the GC if the size is large and we did one recently |
| 286 | // (number of allocations ago) (watch for thread effects) |
| 287 | // DeflateTest allocs a bunch of ~128k buffers w/in 0-5 allocs of each other |
| 288 | // (or, at least, there are only 0-5 objects swept each time) |
| 289 | |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 290 | ptr = dvmHeapSourceAlloc(size); |
| 291 | if (ptr != NULL) { |
| 292 | return ptr; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| Carl Shapiro | ec47e2e | 2010-07-01 17:44:46 -0700 | [diff] [blame] | 295 | /* |
| 296 | * The allocation failed. If the GC is running, block until it |
| 297 | * completes and retry. |
| 298 | */ |
| 299 | if (gDvm.gcHeap->gcRunning) { |
| 300 | /* |
| 301 | * The GC is concurrently tracing the heap. Release the heap |
| 302 | * lock, wait for the GC to complete, and retrying allocating. |
| 303 | */ |
| 304 | dvmWaitForConcurrentGcToComplete(); |
| 305 | ptr = dvmHeapSourceAlloc(size); |
| 306 | if (ptr != NULL) { |
| 307 | return ptr; |
| 308 | } |
| 309 | } |
| 310 | /* |
| 311 | * Another failure. Our thread was starved or there may be too |
| 312 | * many live objects. Try a foreground GC. This will have no |
| 313 | * effect if the concurrent GC is already running. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 314 | */ |
| 315 | gcForMalloc(false); |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 316 | ptr = dvmHeapSourceAlloc(size); |
| 317 | if (ptr != NULL) { |
| 318 | return ptr; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* Even that didn't work; this is an exceptional state. |
| 322 | * Try harder, growing the heap if necessary. |
| 323 | */ |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 324 | ptr = dvmHeapSourceAllocAndGrow(size); |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 325 | if (ptr != NULL) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 326 | size_t newHeapSize; |
| 327 | |
| 328 | newHeapSize = dvmHeapSourceGetIdealFootprint(); |
| 329 | //TODO: may want to grow a little bit more so that the amount of free |
| 330 | // space is equal to the old free space + the utilization slop for |
| 331 | // the new allocation. |
| 332 | LOGI_HEAP("Grow heap (frag case) to " |
| 333 | "%zu.%03zuMB for %zu-byte allocation\n", |
| 334 | FRACTIONAL_MB(newHeapSize), size); |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 335 | return ptr; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* Most allocations should have succeeded by now, so the heap |
| 339 | * is really full, really fragmented, or the requested size is |
| 340 | * really big. Do another GC, collecting SoftReferences this |
| 341 | * time. The VM spec requires that all SoftReferences have |
| 342 | * been collected and cleared before throwing an OOME. |
| 343 | */ |
| 344 | //TODO: wait for the finalizers from the previous GC to finish |
| 345 | collect_soft_refs: |
| 346 | LOGI_HEAP("Forcing collection of SoftReferences for %zu-byte allocation\n", |
| 347 | size); |
| 348 | gcForMalloc(true); |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 349 | ptr = dvmHeapSourceAllocAndGrow(size); |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 350 | if (ptr != NULL) { |
| 351 | return ptr; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 352 | } |
| 353 | //TODO: maybe wait for finalizers and try one last time |
| 354 | |
| 355 | LOGE_HEAP("Out of memory on a %zd-byte allocation.\n", size); |
| 356 | //TODO: tell the HeapSource to dump its state |
| 357 | dvmDumpThread(dvmThreadSelf(), false); |
| 358 | |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | /* Throw an OutOfMemoryError if there's a thread to attach it to. |
| 363 | * Avoid recursing. |
| 364 | * |
| 365 | * The caller must not be holding the heap lock, or else the allocations |
| 366 | * in dvmThrowException() will deadlock. |
| 367 | */ |
| 368 | static void throwOOME() |
| 369 | { |
| 370 | Thread *self; |
| 371 | |
| 372 | if ((self = dvmThreadSelf()) != NULL) { |
| 373 | /* If the current (failing) dvmMalloc() happened as part of thread |
| 374 | * creation/attachment before the thread became part of the root set, |
| 375 | * we can't rely on the thread-local trackedAlloc table, so |
| 376 | * we can't keep track of a real allocated OOME object. But, since |
| 377 | * the thread is in the process of being created, it won't have |
| 378 | * a useful stack anyway, so we may as well make things easier |
| 379 | * by throwing the (stackless) pre-built OOME. |
| 380 | */ |
| 381 | if (dvmIsOnThreadList(self) && !self->throwingOOME) { |
| 382 | /* Let ourselves know that we tried to throw an OOM |
| 383 | * error in the normal way in case we run out of |
| 384 | * memory trying to allocate it inside dvmThrowException(). |
| 385 | */ |
| 386 | self->throwingOOME = true; |
| 387 | |
| 388 | /* Don't include a description string; |
| 389 | * one fewer allocation. |
| 390 | */ |
| 391 | dvmThrowException("Ljava/lang/OutOfMemoryError;", NULL); |
| 392 | } else { |
| 393 | /* |
| 394 | * This thread has already tried to throw an OutOfMemoryError, |
| 395 | * which probably means that we're running out of memory |
| 396 | * while recursively trying to throw. |
| 397 | * |
| 398 | * To avoid any more allocation attempts, "throw" a pre-built |
| 399 | * OutOfMemoryError object (which won't have a useful stack trace). |
| 400 | * |
| 401 | * Note that since this call can't possibly allocate anything, |
| 402 | * we don't care about the state of self->throwingOOME |
| 403 | * (which will usually already be set). |
| 404 | */ |
| 405 | dvmSetException(self, gDvm.outOfMemoryObj); |
| 406 | } |
| 407 | /* We're done with the possible recursion. |
| 408 | */ |
| 409 | self->throwingOOME = false; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Allocate storage on the GC heap. We guarantee 8-byte alignment. |
| 415 | * |
| 416 | * The new storage is zeroed out. |
| 417 | * |
| 418 | * Note that, in rare cases, this could get called while a GC is in |
| 419 | * progress. If a non-VM thread tries to attach itself through JNI, |
| 420 | * it will need to allocate some objects. If this becomes annoying to |
| 421 | * deal with, we can block it at the source, but holding the allocation |
| 422 | * mutex should be enough. |
| 423 | * |
| 424 | * In rare circumstances (JNI AttachCurrentThread) we can be called |
| 425 | * from a non-VM thread. |
| 426 | * |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 427 | * Use ALLOC_DONT_TRACK when we either don't want to track an allocation |
| 428 | * (because it's being done for the interpreter "new" operation and will |
| 429 | * be part of the root set immediately) or we can't (because this allocation |
| 430 | * is for a brand new thread). |
| 431 | * |
| 432 | * Returns NULL and throws an exception on failure. |
| 433 | * |
| 434 | * TODO: don't do a GC if the debugger thinks all threads are suspended |
| 435 | */ |
| 436 | void* dvmMalloc(size_t size, int flags) |
| 437 | { |
| 438 | GcHeap *gcHeap = gDvm.gcHeap; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 439 | void *ptr; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 440 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 441 | dvmLockHeap(); |
| 442 | |
| 443 | /* Try as hard as possible to allocate some memory. |
| 444 | */ |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 445 | ptr = tryMalloc(size); |
| 446 | if (ptr != NULL) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 447 | /* We've got the memory. |
| 448 | */ |
| 449 | if ((flags & ALLOC_FINALIZABLE) != 0) { |
| 450 | /* This object is an instance of a class that |
| 451 | * overrides finalize(). Add it to the finalizable list. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 452 | */ |
| 453 | if (!dvmHeapAddRefToLargeTable(&gcHeap->finalizableRefs, |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 454 | (Object *)ptr)) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 455 | { |
| 456 | LOGE_HEAP("dvmMalloc(): no room for any more " |
| 457 | "finalizable objects\n"); |
| 458 | dvmAbort(); |
| 459 | } |
| 460 | } |
| 461 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 462 | if (gDvm.allocProf.enabled) { |
| 463 | Thread* self = dvmThreadSelf(); |
| 464 | gDvm.allocProf.allocCount++; |
| 465 | gDvm.allocProf.allocSize += size; |
| 466 | if (self != NULL) { |
| 467 | self->allocProf.allocCount++; |
| 468 | self->allocProf.allocSize += size; |
| 469 | } |
| 470 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 471 | } else { |
| 472 | /* The allocation failed. |
| 473 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 474 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 475 | if (gDvm.allocProf.enabled) { |
| 476 | Thread* self = dvmThreadSelf(); |
| 477 | gDvm.allocProf.failedAllocCount++; |
| 478 | gDvm.allocProf.failedAllocSize += size; |
| 479 | if (self != NULL) { |
| 480 | self->allocProf.failedAllocCount++; |
| 481 | self->allocProf.failedAllocSize += size; |
| 482 | } |
| 483 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | dvmUnlockHeap(); |
| 487 | |
| 488 | if (ptr != NULL) { |
| 489 | /* |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 490 | * If caller hasn't asked us not to track it, add it to the |
| 491 | * internal tracking list. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 492 | */ |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 493 | if ((flags & ALLOC_DONT_TRACK) == 0) { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 494 | dvmAddTrackedAlloc((Object*)ptr, NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 495 | } |
| 496 | } else { |
| Ben Cheng | c3b92b2 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 497 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 498 | * The allocation failed; throw an OutOfMemoryError. |
| 499 | */ |
| 500 | throwOOME(); |
| 501 | } |
| 502 | |
| 503 | return ptr; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Returns true iff <obj> points to a valid allocated object. |
| 508 | */ |
| 509 | bool dvmIsValidObject(const Object* obj) |
| 510 | { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 511 | /* Don't bother if it's NULL or not 8-byte aligned. |
| 512 | */ |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 513 | if (obj != NULL && ((uintptr_t)obj & (8-1)) == 0) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 514 | /* Even if the heap isn't locked, this shouldn't return |
| 515 | * any false negatives. The only mutation that could |
| 516 | * be happening is allocation, which means that another |
| 517 | * thread could be in the middle of a read-modify-write |
| 518 | * to add a new bit for a new object. However, that |
| 519 | * RMW will have completed by the time any other thread |
| 520 | * could possibly see the new pointer, so there is no |
| 521 | * danger of dvmIsValidObject() being called on a valid |
| 522 | * pointer whose bit isn't set. |
| 523 | * |
| 524 | * Freeing will only happen during the sweep phase, which |
| 525 | * only happens while the heap is locked. |
| 526 | */ |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 527 | return dvmHeapSourceContains(obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 528 | } |
| 529 | return false; |
| 530 | } |
| 531 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 532 | size_t dvmObjectSizeInHeap(const Object *obj) |
| 533 | { |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 534 | return dvmHeapSourceChunkSize(obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| Carl Shapiro | 6c5dd93 | 2010-08-05 21:49:07 -0700 | [diff] [blame] | 537 | static void verifyRootsAndHeap(void) |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 538 | { |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 539 | dvmVerifyRoots(); |
| 540 | dvmVerifyBitmap(dvmHeapSourceGetLiveBits()); |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 544 | * Raises the scheduling priority of the current thread. Returns the |
| 545 | * original priority if successful. Otherwise, returns INT_MAX on |
| 546 | * failure. |
| 547 | */ |
| 548 | static int raiseThreadPriority(void) |
| 549 | { |
| 550 | /* Get the priority (the "nice" value) of the current thread. The |
| 551 | * getpriority() call can legitimately return -1, so we have to |
| 552 | * explicitly test errno. |
| 553 | */ |
| 554 | errno = 0; |
| 555 | int oldThreadPriority = getpriority(PRIO_PROCESS, 0); |
| 556 | if (errno != 0) { |
| 557 | LOGI_HEAP("getpriority(self) failed: %s", strerror(errno)); |
| 558 | } else if (oldThreadPriority > ANDROID_PRIORITY_NORMAL) { |
| 559 | /* Current value is numerically greater than "normal", which |
| 560 | * in backward UNIX terms means lower priority. |
| 561 | */ |
| 562 | if (oldThreadPriority >= ANDROID_PRIORITY_BACKGROUND) { |
| 563 | set_sched_policy(dvmGetSysThreadId(), SP_FOREGROUND); |
| 564 | } |
| 565 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL) != 0) { |
| 566 | LOGI_HEAP("Unable to elevate priority from %d to %d", |
| 567 | oldThreadPriority, ANDROID_PRIORITY_NORMAL); |
| 568 | } else { |
| 569 | /* |
| 570 | * The priority has been elevated. Return the old value |
| 571 | * so the caller can restore it later. |
| 572 | */ |
| 573 | LOGD_HEAP("Elevating priority from %d to %d", |
| 574 | oldThreadPriority, ANDROID_PRIORITY_NORMAL); |
| 575 | return oldThreadPriority; |
| 576 | } |
| 577 | } |
| 578 | return INT_MAX; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Sets the current thread scheduling priority. |
| 583 | */ |
| 584 | static void setThreadPriority(int newThreadPriority) |
| 585 | { |
| 586 | if (setpriority(PRIO_PROCESS, 0, newThreadPriority) != 0) { |
| 587 | LOGW_HEAP("Unable to reset priority to %d: %s", |
| 588 | newThreadPriority, strerror(errno)); |
| 589 | } else { |
| 590 | LOGD_HEAP("Reset priority to %d", oldThreadPriority); |
| 591 | } |
| 592 | if (newThreadPriority >= ANDROID_PRIORITY_BACKGROUND) { |
| 593 | set_sched_policy(dvmGetSysThreadId(), SP_BACKGROUND); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 598 | * Initiate garbage collection. |
| 599 | * |
| 600 | * NOTES: |
| 601 | * - If we don't hold gDvm.threadListLock, it's possible for a thread to |
| 602 | * be added to the thread list while we work. The thread should NOT |
| 603 | * start executing, so this is only interesting when we start chasing |
| 604 | * thread stacks. (Before we do so, grab the lock.) |
| 605 | * |
| 606 | * We are not allowed to GC when the debugger has suspended the VM, which |
| 607 | * is awkward because debugger requests can cause allocations. The easiest |
| 608 | * way to enforce this is to refuse to GC on an allocation made by the |
| 609 | * JDWP thread -- we have to expand the heap or fail. |
| 610 | */ |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 611 | void dvmCollectGarbageInternal(const GcSpec* spec) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 612 | { |
| 613 | GcHeap *gcHeap = gDvm.gcHeap; |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 614 | u4 rootSuspend, rootSuspendTime, rootStart = 0 , rootEnd = 0; |
| 615 | u4 dirtySuspend, dirtyStart = 0, dirtyEnd = 0; |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 616 | u4 totalTime; |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 617 | size_t numObjectsFreed, numBytesFreed; |
| 618 | size_t currAllocated, currFootprint; |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 619 | size_t percentFree; |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 620 | int oldThreadPriority = INT_MAX; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 621 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 622 | /* The heap lock must be held. |
| 623 | */ |
| 624 | |
| 625 | if (gcHeap->gcRunning) { |
| 626 | LOGW_HEAP("Attempted recursive GC\n"); |
| 627 | return; |
| 628 | } |
| Carl Shapiro | 03f3b13 | 2010-07-27 17:25:31 -0700 | [diff] [blame] | 629 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 630 | gcHeap->gcRunning = true; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 631 | |
| Andy McFadden | 58aa611 | 2011-01-19 14:48:52 -0800 | [diff] [blame] | 632 | /* |
| 633 | * Grab the heapWorkerLock to prevent the HeapWorker thread from |
| 634 | * doing work. If it's executing a finalizer or an enqueue operation |
| 635 | * it won't be holding the lock, so this should return quickly. |
| 636 | */ |
| 637 | dvmLockMutex(&gDvm.heapWorkerLock); |
| 638 | |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 639 | rootSuspend = dvmGetRelativeTimeMsec(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 640 | dvmSuspendAllThreads(SUSPEND_FOR_GC); |
| Carl Shapiro | 03f3b13 | 2010-07-27 17:25:31 -0700 | [diff] [blame] | 641 | rootStart = dvmGetRelativeTimeMsec(); |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 642 | rootSuspendTime = rootStart - rootSuspend; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 643 | |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 644 | /* |
| 645 | * If we are not marking concurrently raise the priority of the |
| 646 | * thread performing the garbage collection. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 647 | */ |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 648 | if (!spec->isConcurrent) { |
| 649 | oldThreadPriority = raiseThreadPriority(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 652 | /* Make sure that the HeapWorker thread hasn't become |
| 653 | * wedged inside interp code. If it has, this call will |
| 654 | * print a message and abort the VM. |
| 655 | */ |
| 656 | dvmAssertHeapWorkerThreadRunning(); |
| 657 | |
| 658 | /* Lock the pendingFinalizationRefs list. |
| 659 | * |
| 660 | * Acquire the lock after suspending so the finalizer |
| 661 | * thread can't block in the RUNNING state while |
| 662 | * we try to suspend. |
| 663 | */ |
| 664 | dvmLockMutex(&gDvm.heapWorkerListLock); |
| 665 | |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 666 | if (gDvm.preVerify) { |
| Carl Shapiro | 6c5dd93 | 2010-08-05 21:49:07 -0700 | [diff] [blame] | 667 | LOGV_HEAP("Verifying roots and heap before GC"); |
| 668 | verifyRootsAndHeap(); |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 671 | dvmMethodTraceGCBegin(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 672 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 673 | /* Set up the marking context. |
| 674 | */ |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 675 | if (!dvmHeapBeginMarkStep(spec->isPartial)) { |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 676 | LOGE_HEAP("dvmHeapBeginMarkStep failed; aborting\n"); |
| 677 | dvmAbort(); |
| 678 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 679 | |
| 680 | /* Mark the set of objects that are strongly reachable from the roots. |
| 681 | */ |
| 682 | LOGD_HEAP("Marking..."); |
| 683 | dvmHeapMarkRootSet(); |
| 684 | |
| 685 | /* dvmHeapScanMarkedObjects() will build the lists of known |
| 686 | * instances of the Reference classes. |
| 687 | */ |
| 688 | gcHeap->softReferences = NULL; |
| 689 | gcHeap->weakReferences = NULL; |
| 690 | gcHeap->phantomReferences = NULL; |
| 691 | |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 692 | if (spec->isConcurrent) { |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 693 | /* |
| Carl Shapiro | ec47e2e | 2010-07-01 17:44:46 -0700 | [diff] [blame] | 694 | * Resume threads while tracing from the roots. We unlock the |
| 695 | * heap to allow mutator threads to allocate from free space. |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 696 | */ |
| Carl Shapiro | 03f3b13 | 2010-07-27 17:25:31 -0700 | [diff] [blame] | 697 | rootEnd = dvmGetRelativeTimeMsec(); |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 698 | dvmClearCardTable(); |
| Carl Shapiro | ec47e2e | 2010-07-01 17:44:46 -0700 | [diff] [blame] | 699 | dvmUnlockHeap(); |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 700 | dvmResumeAllThreads(SUSPEND_FOR_GC); |
| 701 | } |
| 702 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 703 | /* Recursively mark any objects that marked objects point to strongly. |
| 704 | * If we're not collecting soft references, soft-reachable |
| 705 | * objects will also be marked. |
| 706 | */ |
| 707 | LOGD_HEAP("Recursing..."); |
| 708 | dvmHeapScanMarkedObjects(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 709 | |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 710 | if (spec->isConcurrent) { |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 711 | /* |
| Carl Shapiro | ec47e2e | 2010-07-01 17:44:46 -0700 | [diff] [blame] | 712 | * Re-acquire the heap lock and perform the final thread |
| 713 | * suspension. |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 714 | */ |
| Carl Shapiro | ec47e2e | 2010-07-01 17:44:46 -0700 | [diff] [blame] | 715 | dvmLockHeap(); |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 716 | dirtySuspend = dvmGetRelativeTimeMsec(); |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 717 | dvmSuspendAllThreads(SUSPEND_FOR_GC); |
| Carl Shapiro | 03f3b13 | 2010-07-27 17:25:31 -0700 | [diff] [blame] | 718 | dirtyStart = dvmGetRelativeTimeMsec(); |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 719 | /* |
| 720 | * As no barrier intercepts root updates, we conservatively |
| 721 | * assume all roots may be gray and re-mark them. |
| 722 | */ |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 723 | dvmHeapReMarkRootSet(); |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 724 | /* |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 725 | * With the exception of reference objects and weak interned |
| 726 | * strings, all gray objects should now be on dirty cards. |
| 727 | */ |
| 728 | if (gDvm.verifyCardTable) { |
| 729 | dvmVerifyCardTable(); |
| 730 | } |
| 731 | /* |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 732 | * Recursively mark gray objects pointed to by the roots or by |
| 733 | * heap objects dirtied during the concurrent mark. |
| 734 | */ |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 735 | dvmHeapReScanMarkedObjects(); |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 738 | /* |
| 739 | * All strongly-reachable objects have now been marked. Process |
| 740 | * weakly-reachable objects discovered while tracing. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 741 | */ |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 742 | dvmHeapProcessReferences(&gcHeap->softReferences, |
| 743 | spec->softReferencePolicy == CLEAR, |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 744 | &gcHeap->weakReferences, |
| 745 | &gcHeap->phantomReferences); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 746 | |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 747 | #if defined(WITH_JIT) |
| 748 | /* |
| 749 | * Patching a chaining cell is very cheap as it only updates 4 words. It's |
| 750 | * the overhead of stopping all threads and synchronizing the I/D cache |
| 751 | * that makes it expensive. |
| 752 | * |
| 753 | * Therefore we batch those work orders in a queue and go through them |
| 754 | * when threads are suspended for GC. |
| 755 | */ |
| 756 | dvmCompilerPerformSafePointChecks(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 757 | #endif |
| 758 | |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 759 | LOGD_HEAP("Sweeping..."); |
| 760 | |
| 761 | dvmHeapSweepSystemWeaks(); |
| 762 | |
| 763 | /* |
| 764 | * Live objects have a bit set in the mark bitmap, swap the mark |
| 765 | * and live bitmaps. The sweep can proceed concurrently viewing |
| 766 | * the new live bitmap as the old mark bitmap, and vice versa. |
| 767 | */ |
| 768 | dvmHeapSourceSwapBitmaps(); |
| 769 | |
| 770 | if (gDvm.postVerify) { |
| 771 | LOGV_HEAP("Verifying roots and heap after GC"); |
| 772 | verifyRootsAndHeap(); |
| 773 | } |
| 774 | |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 775 | if (spec->isConcurrent) { |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 776 | dirtyEnd = dvmGetRelativeTimeMsec(); |
| 777 | dvmUnlockHeap(); |
| 778 | dvmResumeAllThreads(SUSPEND_FOR_GC); |
| 779 | } |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 780 | dvmHeapSweepUnmarkedObjects(spec->isPartial, spec->isConcurrent, |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 781 | &numObjectsFreed, &numBytesFreed); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 782 | LOGD_HEAP("Cleaning up..."); |
| 783 | dvmHeapFinishMarkStep(); |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 784 | if (spec->isConcurrent) { |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 785 | dvmLockHeap(); |
| 786 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 787 | |
| 788 | LOGD_HEAP("Done."); |
| 789 | |
| 790 | /* Now's a good time to adjust the heap size, since |
| 791 | * we know what our utilization is. |
| 792 | * |
| 793 | * This doesn't actually resize any memory; |
| 794 | * it just lets the heap grow more when necessary. |
| 795 | */ |
| Carl Shapiro | e7bdd8b | 2010-12-17 15:34:52 -0800 | [diff] [blame] | 796 | dvmHeapSourceGrowForUtilization(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 797 | |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 798 | currAllocated = dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0); |
| 799 | currFootprint = dvmHeapSourceGetValue(HS_FOOTPRINT, NULL, 0); |
| 800 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 801 | /* Now that we've freed up the GC heap, return any large |
| 802 | * free chunks back to the system. They'll get paged back |
| 803 | * in the next time they're used. Don't do it immediately, |
| 804 | * though; if the process is still allocating a bunch of |
| 805 | * memory, we'll be taking a ton of page faults that we don't |
| 806 | * necessarily need to. |
| 807 | * |
| 808 | * Cancel any old scheduled trims, and schedule a new one. |
| 809 | */ |
| 810 | dvmScheduleHeapSourceTrim(5); // in seconds |
| 811 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 812 | dvmMethodTraceGCEnd(); |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 813 | LOGV_HEAP("GC finished"); |
| 814 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 815 | gcHeap->gcRunning = false; |
| 816 | |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 817 | LOGV_HEAP("Resuming threads"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 818 | dvmUnlockMutex(&gDvm.heapWorkerListLock); |
| 819 | dvmUnlockMutex(&gDvm.heapWorkerLock); |
| 820 | |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 821 | if (spec->isConcurrent) { |
| Carl Shapiro | ec47e2e | 2010-07-01 17:44:46 -0700 | [diff] [blame] | 822 | /* |
| 823 | * Wake-up any threads that blocked after a failed allocation |
| 824 | * request. |
| 825 | */ |
| 826 | dvmBroadcastCond(&gDvm.gcHeapCond); |
| 827 | } |
| 828 | |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 829 | if (!spec->isConcurrent) { |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 830 | dirtyEnd = dvmGetRelativeTimeMsec(); |
| 831 | dvmResumeAllThreads(SUSPEND_FOR_GC); |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 832 | /* |
| 833 | * Restore the original thread scheduling priority if it was |
| 834 | * changed at the start of the current garbage collection. |
| 835 | */ |
| 836 | if (oldThreadPriority != INT_MAX) { |
| 837 | setThreadPriority(oldThreadPriority); |
| San Mehat | 256fc15 | 2009-04-21 14:03:06 -0700 | [diff] [blame] | 838 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 839 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 840 | |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 841 | percentFree = 100 - (size_t)(100.0f * (float)currAllocated / currFootprint); |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 842 | if (!spec->isConcurrent) { |
| Carl Shapiro | 03f3b13 | 2010-07-27 17:25:31 -0700 | [diff] [blame] | 843 | u4 markSweepTime = dirtyEnd - rootStart; |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 844 | bool isSmall = numBytesFreed > 0 && numBytesFreed < 1024; |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 845 | totalTime = rootSuspendTime + markSweepTime; |
| Carl Shapiro | e7bdd8b | 2010-12-17 15:34:52 -0800 | [diff] [blame] | 846 | LOGD("%s freed %s%zdK, %d%% free %zdK/%zdK, paused %ums", |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 847 | spec->reason, |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 848 | isSmall ? "<" : "", |
| 849 | numBytesFreed ? MAX(numBytesFreed / 1024, 1) : 0, |
| 850 | percentFree, |
| 851 | currAllocated / 1024, currFootprint / 1024, |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 852 | markSweepTime); |
| Carl Shapiro | 03f3b13 | 2010-07-27 17:25:31 -0700 | [diff] [blame] | 853 | } else { |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 854 | u4 rootTime = rootEnd - rootStart; |
| 855 | u4 dirtySuspendTime = dirtyStart - dirtySuspend; |
| 856 | u4 dirtyTime = dirtyEnd - dirtyStart; |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 857 | bool isSmall = numBytesFreed > 0 && numBytesFreed < 1024; |
| Carl Shapiro | 03f3b13 | 2010-07-27 17:25:31 -0700 | [diff] [blame] | 858 | totalTime = rootSuspendTime + rootTime + dirtySuspendTime + dirtyTime; |
| Carl Shapiro | e7bdd8b | 2010-12-17 15:34:52 -0800 | [diff] [blame] | 859 | LOGD("%s freed %s%zdK, %d%% free %zdK/%zdK, paused %ums+%ums", |
| Carl Shapiro | cc6f511 | 2011-01-26 17:25:27 -0800 | [diff] [blame] | 860 | spec->reason, |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 861 | isSmall ? "<" : "", |
| 862 | numBytesFreed ? MAX(numBytesFreed / 1024, 1) : 0, |
| 863 | percentFree, |
| 864 | currAllocated / 1024, currFootprint / 1024, |
| Carl Shapiro | 570942c | 2010-09-17 15:53:16 -0700 | [diff] [blame] | 865 | rootTime, dirtyTime); |
| Carl Shapiro | 03f3b13 | 2010-07-27 17:25:31 -0700 | [diff] [blame] | 866 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 867 | if (gcHeap->ddmHpifWhen != 0) { |
| 868 | LOGD_HEAP("Sending VM heap info to DDM\n"); |
| 869 | dvmDdmSendHeapInfo(gcHeap->ddmHpifWhen, false); |
| 870 | } |
| 871 | if (gcHeap->ddmHpsgWhen != 0) { |
| 872 | LOGD_HEAP("Dumping VM heap to DDM\n"); |
| 873 | dvmDdmSendHeapSegments(false, false); |
| 874 | } |
| 875 | if (gcHeap->ddmNhsgWhen != 0) { |
| 876 | LOGD_HEAP("Dumping native heap to DDM\n"); |
| 877 | dvmDdmSendHeapSegments(false, true); |
| 878 | } |
| 879 | } |
| 880 | |
| Andy McFadden | 58aa611 | 2011-01-19 14:48:52 -0800 | [diff] [blame] | 881 | /* |
| 882 | * If the concurrent GC is running, wait for it to finish. The caller |
| 883 | * must hold the heap lock. |
| 884 | * |
| 885 | * Note: the second dvmChangeStatus() could stall if we were in RUNNING |
| 886 | * on entry, and some other thread has asked us to suspend. In that |
| 887 | * case we will be suspended with the heap lock held, which can lead to |
| 888 | * deadlock if the other thread tries to do something with the managed heap. |
| 889 | * For example, the debugger might suspend us and then execute a method that |
| 890 | * allocates memory. We can avoid this situation by releasing the lock |
| 891 | * before self-suspending. (The developer can work around this specific |
| 892 | * situation by single-stepping the VM. Alternatively, we could disable |
| 893 | * concurrent GC when the debugger is attached, but that might change |
| 894 | * behavior more than is desirable.) |
| 895 | * |
| 896 | * This should not be a problem in production, because any GC-related |
| 897 | * activity will grab the lock before issuing a suspend-all. (We may briefly |
| 898 | * suspend when the GC thread calls dvmUnlockHeap before dvmResumeAllThreads, |
| 899 | * but there's no risk of deadlock.) |
| 900 | */ |
| Carl Shapiro | ec47e2e | 2010-07-01 17:44:46 -0700 | [diff] [blame] | 901 | void dvmWaitForConcurrentGcToComplete(void) |
| 902 | { |
| 903 | Thread *self = dvmThreadSelf(); |
| Carl Shapiro | ec47e2e | 2010-07-01 17:44:46 -0700 | [diff] [blame] | 904 | assert(self != NULL); |
| Carl Shapiro | 039167e | 2010-12-20 18:33:24 -0800 | [diff] [blame] | 905 | while (gDvm.gcHeap->gcRunning) { |
| 906 | ThreadStatus oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); |
| 907 | dvmWaitCond(&gDvm.gcHeapCond, &gDvm.gcHeapLock); |
| 908 | dvmChangeStatus(self, oldStatus); |
| 909 | } |
| Carl Shapiro | ec47e2e | 2010-07-01 17:44:46 -0700 | [diff] [blame] | 910 | } |