Heap.c revision 03f3b1394cc8421d125fd00455858944f0e9808d
1f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 2f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project 3f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 4f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 5f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * you may not use this file except in compliance with the License. 6f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * You may obtain a copy of the License at 7f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 8f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 10f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * See the License for the specific language governing permissions and 14f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * limitations under the License. 15f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 16f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 17f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Garbage-collecting memory allocator. 18f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 19f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "Dalvik.h" 20962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes#include "alloc/HeapBitmap.h" 21962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes#include "alloc/Verify.h" 22f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/HeapTable.h" 23f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/Heap.h" 24f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/HeapInternal.h" 25f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/DdmHeap.h" 26f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/HeapSource.h" 27f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/MarkSweep.h" 28f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 29f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "utils/threads.h" // need Android thread priorities 30f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#define kInvalidPriority 10000 31f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 325a2056ca8c7e7c0ed9f51d68cedbee59cc936685San Mehat#include <cutils/sched_policy.h> 335a2056ca8c7e7c0ed9f51d68cedbee59cc936685San Mehat 34f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include <sys/time.h> 35f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include <sys/resource.h> 36f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include <limits.h> 37f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include <errno.h> 38f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 391b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayesstatic const char* GcReasonStr[] = { 401b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes [GC_FOR_MALLOC] = "GC_FOR_MALLOC", 41ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro [GC_CONCURRENT] = "GC_CONCURRENT", 421b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes [GC_EXPLICIT] = "GC_EXPLICIT", 431b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes [GC_EXTERNAL_ALLOC] = "GC_EXTERNAL_ALLOC", 441b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes [GC_HPROF_DUMP_HEAP] = "GC_HPROF_DUMP_HEAP" 451b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes}; 461b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes 47f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 48f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Initialize the GC heap. 49f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 50f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Returns true if successful, false otherwise. 51f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 52f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectbool dvmHeapStartup() 53f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 54f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project GcHeap *gcHeap; 55f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 56f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if defined(WITH_ALLOC_LIMITS) 57f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.checkAllocLimits = false; 58f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocationLimit = -1; 59f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 60f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 61f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap = dvmHeapSourceStartup(gDvm.heapSizeStart, gDvm.heapSizeMax); 62f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap == NULL) { 63f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return false; 64f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 65f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->heapWorkerCurrentObject = NULL; 66f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->heapWorkerCurrentMethod = NULL; 67f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->heapWorkerInterpStartTime = 0LL; 68f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmHpifWhen = 0; 69f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmHpsgWhen = 0; 70f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmHpsgWhat = 0; 71f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmNhsgWhen = 0; 72f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmNhsgWhat = 0; 73f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if WITH_HPROF 74f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofDumpOnGc = false; 75f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofContext = NULL; 76f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 77f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.gcHeap = gcHeap; 78f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 79f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Set up the lists and lock we'll use for finalizable 80f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * and reference objects. 81f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 82f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmInitMutex(&gDvm.heapWorkerListLock); 83f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->finalizableRefs = NULL; 84f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->pendingFinalizationRefs = NULL; 85f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->referenceOperations = NULL; 86f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 87b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes if (!dvmCardTableStartup()) { 88b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes LOGE_HEAP("card table startup failed."); 89b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes return false; 90b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes } 91b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes 92f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Initialize the HeapWorker locks and other state 93f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * that the GC uses. 94f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 95f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmInitializeHeapWorkerState(); 96f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 97f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return true; 98f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 99f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 100ec805eaed940e40212e85b58b163c7649feaca56Carl Shapirobool dvmHeapStartupAfterZygote(void) 101f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 102f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Update our idea of the last GC start time so that we 103f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * don't use the last time that Zygote happened to GC. 104f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 105f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.gcHeap->gcStartTime = dvmGetRelativeTimeUsec(); 106ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro return dvmHeapSourceStartupAfterZygote(); 107f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 108f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 109f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmHeapShutdown() 110f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 111f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: make sure we're locked 112f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.gcHeap != NULL) { 113b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes dvmCardTableShutdown(); 114b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes /* Tables are allocated on the native heap; they need to be 115b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes * cleaned up explicitly. The process may stick around, so we 116b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes * don't want to leak any native memory. 117f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 118a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapFreeLargeTable(gDvm.gcHeap->finalizableRefs); 119a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro gDvm.gcHeap->finalizableRefs = NULL; 120f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 121a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapFreeLargeTable(gDvm.gcHeap->pendingFinalizationRefs); 122a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro gDvm.gcHeap->pendingFinalizationRefs = NULL; 123f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 124a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapFreeLargeTable(gDvm.gcHeap->referenceOperations); 125a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro gDvm.gcHeap->referenceOperations = NULL; 126f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 127b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes /* Destroy the heap. Any outstanding pointers will point to 128b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes * unmapped memory (unless/until someone else maps it). This 129b874ab98306a109c4988bb1cde687a24f4f8201fBarry Hayes * frees gDvm.gcHeap as a side-effect. 130f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 131a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapSourceShutdown(&gDvm.gcHeap); 132f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 133f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 134f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 135f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 136ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * Shutdown any threads internal to the heap. 137ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro */ 138ec805eaed940e40212e85b58b163c7649feaca56Carl Shapirovoid dvmHeapThreadShutdown(void) 139ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro{ 140ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro dvmHeapSourceThreadShutdown(); 141ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro} 142ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro 143ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro/* 144f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * We've been asked to allocate something we can't, e.g. an array so 1456da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * large that (length * elementWidth) is larger than 2^31. 146f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 1476da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * _The Java Programming Language_, 4th edition, says, "you can be sure 1486da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * that all SoftReferences to softly reachable objects will be cleared 1496da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * before an OutOfMemoryError is thrown." 1506da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * 1516da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * It's unclear whether that holds for all situations where an OOM can 1526da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * be thrown, or just in the context of an allocation that fails due 1536da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * to lack of heap space. For simplicity we just throw the exception. 1546da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * 1556da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * (OOM due to actually running out of space is handled elsewhere.) 156f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 157f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmThrowBadAllocException(const char* msg) 158f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 1596da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden dvmThrowException("Ljava/lang/OutOfMemoryError;", msg); 160f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 161f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 162f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 163f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Grab the lock, but put ourselves into THREAD_VMWAIT if it looks like 164f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we're going to have to wait on the mutex. 165f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 166f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectbool dvmLockHeap() 167f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 168980ffb0243a1840ad0a93cfa06dfc02ca6f2d01cCarl Shapiro if (dvmTryLockMutex(&gDvm.gcHeapLock) != 0) { 169f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread *self; 170f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project ThreadStatus oldStatus; 171f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 172f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self = dvmThreadSelf(); 1735617ad30c611f373e16bf10c0feec114faef54efCarl Shapiro oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); 174980ffb0243a1840ad0a93cfa06dfc02ca6f2d01cCarl Shapiro dvmLockMutex(&gDvm.gcHeapLock); 1755617ad30c611f373e16bf10c0feec114faef54efCarl Shapiro dvmChangeStatus(self, oldStatus); 176f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 177f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 178f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return true; 179f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 180f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 181f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmUnlockHeap() 182f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 183f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.gcHeapLock); 184f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 185f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 186f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Pop an object from the list of pending finalizations and 187f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * reference clears/enqueues, and return the object. 188f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The caller must call dvmReleaseTrackedAlloc() 189f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * on the object when finished. 190f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 191f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Typically only called by the heap worker thread. 192f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 193f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source ProjectObject *dvmGetNextHeapWorkerObject(HeapWorkerOperation *op) 194f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 195f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Object *obj; 196f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project GcHeap *gcHeap = gDvm.gcHeap; 197f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 198f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project assert(op != NULL); 199f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 200f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockMutex(&gDvm.heapWorkerListLock); 201f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 202f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project obj = dvmHeapGetNextObjectFromLargeTable(&gcHeap->referenceOperations); 203f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (obj != NULL) { 204646ba0996da817deddb509b2130a6ad432096691Carl Shapiro *op = WORKER_ENQUEUE; 205f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 206f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project obj = dvmHeapGetNextObjectFromLargeTable( 207f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project &gcHeap->pendingFinalizationRefs); 208f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (obj != NULL) { 209f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *op = WORKER_FINALIZE; 210f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 211f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 212f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 213f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (obj != NULL) { 214f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Don't let the GC collect the object until the 215f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * worker thread is done with it. 216f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 217f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAddTrackedAlloc(obj, NULL); 218f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 219f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 220f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.heapWorkerListLock); 221f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 222f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return obj; 223f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 224f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 225f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Whenever the effective heap size may have changed, 226f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * this function must be called. 227f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 228f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmHeapSizeChanged() 229f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 230f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 231f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 232f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Do a full garbage collection, which may grow the 233f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * heap as a side-effect if the live set is large. 234f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 235f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectstatic void gcForMalloc(bool collectSoftReferences) 236f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 237f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 238f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.allocProf.enabled) { 239f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread* self = dvmThreadSelf(); 240f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.gcCount++; 241f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 242f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.gcCount++; 243f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 244f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 245f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 246f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* This may adjust the soft limit as a side-effect. 247f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 248f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("dvmMalloc initiating GC%s\n", 249f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project collectSoftReferences ? "(collect SoftReferences)" : ""); 2501b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes dvmCollectGarbageInternal(collectSoftReferences, GC_FOR_MALLOC); 251f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 252f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 253f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Try as hard as possible to allocate some memory. 254f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 2556343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapirostatic void *tryMalloc(size_t size) 256f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 2576343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro void *ptr; 258f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 259f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Don't try too hard if there's no way the allocation is 260f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * going to succeed. We have to collect SoftReferences before 261f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * throwing an OOME, though. 262f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 263f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (size >= gDvm.heapSizeMax) { 264f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGW_HEAP("dvmMalloc(%zu/0x%08zx): " 265f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "someone's allocating a huge buffer\n", size, size); 2666343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = NULL; 267f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project goto collect_soft_refs; 268f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 269f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 270f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: figure out better heuristics 271f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// There will be a lot of churn if someone allocates a bunch of 272f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// big objects in a row, and we hit the frag case each time. 273f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// A full GC for each. 274f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// Maybe we grow the heap in bigger leaps 275f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// Maybe we skip the GC if the size is large and we did one recently 276f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// (number of allocations ago) (watch for thread effects) 277f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// DeflateTest allocs a bunch of ~128k buffers w/in 0-5 allocs of each other 278f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// (or, at least, there are only 0-5 objects swept each time) 279f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 2806343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = dvmHeapSourceAlloc(size); 2816343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 2826343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return ptr; 283f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 284f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 285ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro /* 286ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * The allocation failed. If the GC is running, block until it 287ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * completes and retry. 288ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro */ 289ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro if (gDvm.gcHeap->gcRunning) { 290ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro /* 291ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * The GC is concurrently tracing the heap. Release the heap 292ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * lock, wait for the GC to complete, and retrying allocating. 293ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro */ 294ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro dvmWaitForConcurrentGcToComplete(); 295ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro ptr = dvmHeapSourceAlloc(size); 296ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro if (ptr != NULL) { 297ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro return ptr; 298ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro } 299ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro } 300ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro /* 301ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * Another failure. Our thread was starved or there may be too 302ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * many live objects. Try a foreground GC. This will have no 303ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * effect if the concurrent GC is already running. 304f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 305f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcForMalloc(false); 3066343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = dvmHeapSourceAlloc(size); 3076343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 3086343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return ptr; 309f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 310f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 311f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Even that didn't work; this is an exceptional state. 312f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Try harder, growing the heap if necessary. 313f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 3146343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = dvmHeapSourceAllocAndGrow(size); 315f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapSizeChanged(); 3166343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 317f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t newHeapSize; 318f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 319f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project newHeapSize = dvmHeapSourceGetIdealFootprint(); 320f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: may want to grow a little bit more so that the amount of free 321f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// space is equal to the old free space + the utilization slop for 322f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// the new allocation. 323f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGI_HEAP("Grow heap (frag case) to " 324f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "%zu.%03zuMB for %zu-byte allocation\n", 325f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project FRACTIONAL_MB(newHeapSize), size); 3266343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return ptr; 327f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 328f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 329f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Most allocations should have succeeded by now, so the heap 330f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * is really full, really fragmented, or the requested size is 331f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * really big. Do another GC, collecting SoftReferences this 332f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * time. The VM spec requires that all SoftReferences have 333f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * been collected and cleared before throwing an OOME. 334f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 335f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: wait for the finalizers from the previous GC to finish 336f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectcollect_soft_refs: 337f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGI_HEAP("Forcing collection of SoftReferences for %zu-byte allocation\n", 338f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size); 339f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcForMalloc(true); 3406343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = dvmHeapSourceAllocAndGrow(size); 341f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapSizeChanged(); 3426343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 3436343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return ptr; 344f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 345f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: maybe wait for finalizers and try one last time 346f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 347f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGE_HEAP("Out of memory on a %zd-byte allocation.\n", size); 348f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: tell the HeapSource to dump its state 349f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDumpThread(dvmThreadSelf(), false); 350f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 351f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return NULL; 352f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 353f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 354f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Throw an OutOfMemoryError if there's a thread to attach it to. 355f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Avoid recursing. 356f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 357f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The caller must not be holding the heap lock, or else the allocations 358f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * in dvmThrowException() will deadlock. 359f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 360f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectstatic void throwOOME() 361f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 362f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread *self; 363f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 364f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if ((self = dvmThreadSelf()) != NULL) { 365f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* If the current (failing) dvmMalloc() happened as part of thread 366f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * creation/attachment before the thread became part of the root set, 367f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we can't rely on the thread-local trackedAlloc table, so 368f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we can't keep track of a real allocated OOME object. But, since 369f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * the thread is in the process of being created, it won't have 370f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * a useful stack anyway, so we may as well make things easier 371f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * by throwing the (stackless) pre-built OOME. 372f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 373f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (dvmIsOnThreadList(self) && !self->throwingOOME) { 374f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Let ourselves know that we tried to throw an OOM 375f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * error in the normal way in case we run out of 376f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * memory trying to allocate it inside dvmThrowException(). 377f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 378f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->throwingOOME = true; 379f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 380f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Don't include a description string; 381f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * one fewer allocation. 382f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 383f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmThrowException("Ljava/lang/OutOfMemoryError;", NULL); 384f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 385f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* 386f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * This thread has already tried to throw an OutOfMemoryError, 387f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * which probably means that we're running out of memory 388f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * while recursively trying to throw. 389f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 390f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * To avoid any more allocation attempts, "throw" a pre-built 391f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * OutOfMemoryError object (which won't have a useful stack trace). 392f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 393f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Note that since this call can't possibly allocate anything, 394f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we don't care about the state of self->throwingOOME 395f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * (which will usually already be set). 396f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 397f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmSetException(self, gDvm.outOfMemoryObj); 398f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 399f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* We're done with the possible recursion. 400f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 401f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->throwingOOME = false; 402f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 403f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 404f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 405f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 406f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Allocate storage on the GC heap. We guarantee 8-byte alignment. 407f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 408f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The new storage is zeroed out. 409f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 410f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Note that, in rare cases, this could get called while a GC is in 411f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * progress. If a non-VM thread tries to attach itself through JNI, 412f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * it will need to allocate some objects. If this becomes annoying to 413f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * deal with, we can block it at the source, but holding the allocation 414f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * mutex should be enough. 415f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 416f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * In rare circumstances (JNI AttachCurrentThread) we can be called 417f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * from a non-VM thread. 418f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 419f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Use ALLOC_DONT_TRACK when we either don't want to track an allocation 420f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * (because it's being done for the interpreter "new" operation and will 421f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * be part of the root set immediately) or we can't (because this allocation 422f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * is for a brand new thread). 423f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 424f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Returns NULL and throws an exception on failure. 425f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 426f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * TODO: don't do a GC if the debugger thinks all threads are suspended 427f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 428f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid* dvmMalloc(size_t size, int flags) 429f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 430f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project GcHeap *gcHeap = gDvm.gcHeap; 431f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project void *ptr; 432f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 433f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if defined(WITH_ALLOC_LIMITS) 434f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* 435f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * See if they've exceeded the allocation limit for this thread. 436f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 437f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * A limit value of -1 means "no limit". 438f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 439f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * This is enabled at compile time because it requires us to do a 440f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * TLS lookup for the Thread pointer. This has enough of a performance 441f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * impact that we don't want to do it if we don't have to. (Now that 442f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we're using gDvm.checkAllocLimits we may want to reconsider this, 443f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * but it's probably still best to just compile the check out of 444f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * production code -- one less thing to hit on every allocation.) 445f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 446f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.checkAllocLimits) { 447f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread* self = dvmThreadSelf(); 448f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 449f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int count = self->allocLimit; 450f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (count > 0) { 451f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocLimit--; 452f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else if (count == 0) { 453f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* fail! */ 454f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project assert(!gDvm.initializing); 455f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocLimit = -1; 456f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmThrowException("Ldalvik/system/AllocationLimitError;", 457f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "thread allocation limit exceeded"); 458f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return NULL; 459f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 460f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 461f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 462f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 463f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.allocationLimit >= 0) { 464f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project assert(!gDvm.initializing); 465f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocationLimit = -1; 466f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmThrowException("Ldalvik/system/AllocationLimitError;", 467f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "global allocation limit exceeded"); 468f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return NULL; 469f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 470f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 471f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 472f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockHeap(); 473f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 474f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Try as hard as possible to allocate some memory. 475f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 4766343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = tryMalloc(size); 4776343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 478f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* We've got the memory. 479f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 480f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if ((flags & ALLOC_FINALIZABLE) != 0) { 481f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* This object is an instance of a class that 482f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * overrides finalize(). Add it to the finalizable list. 483f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 484f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (!dvmHeapAddRefToLargeTable(&gcHeap->finalizableRefs, 4856343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro (Object *)ptr)) 486f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project { 487f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGE_HEAP("dvmMalloc(): no room for any more " 488f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "finalizable objects\n"); 489f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAbort(); 490f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 491f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 492f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 493f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 494f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.allocProf.enabled) { 495f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread* self = dvmThreadSelf(); 496f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.allocCount++; 497f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.allocSize += size; 498f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 499f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.allocCount++; 500f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.allocSize += size; 501f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 502f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 503f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 504f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 505f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* The allocation failed. 506f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 507f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 508f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 509f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.allocProf.enabled) { 510f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread* self = dvmThreadSelf(); 511f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.failedAllocCount++; 512f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.failedAllocSize += size; 513f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 514f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.failedAllocCount++; 515f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.failedAllocSize += size; 516f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 517f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 518f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 519f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 520f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 521f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockHeap(); 522f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 523f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (ptr != NULL) { 524f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* 525d4f78d3a764e6aa8f7174c78f537c016dac7f7ecBarry Hayes * If caller hasn't asked us not to track it, add it to the 526d4f78d3a764e6aa8f7174c78f537c016dac7f7ecBarry Hayes * internal tracking list. 527f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 528d4f78d3a764e6aa8f7174c78f537c016dac7f7ecBarry Hayes if ((flags & ALLOC_DONT_TRACK) == 0) { 529f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAddTrackedAlloc(ptr, NULL); 530f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 531f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 532c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng /* 533f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The allocation failed; throw an OutOfMemoryError. 534f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 535f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project throwOOME(); 536f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 537f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 538f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return ptr; 539f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 540f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 541f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 542f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Returns true iff <obj> points to a valid allocated object. 543f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 544f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectbool dvmIsValidObject(const Object* obj) 545f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 546f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Don't bother if it's NULL or not 8-byte aligned. 547f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 5486343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (obj != NULL && ((uintptr_t)obj & (8-1)) == 0) { 549f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Even if the heap isn't locked, this shouldn't return 550f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * any false negatives. The only mutation that could 551f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * be happening is allocation, which means that another 552f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * thread could be in the middle of a read-modify-write 553f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * to add a new bit for a new object. However, that 554f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * RMW will have completed by the time any other thread 555f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * could possibly see the new pointer, so there is no 556f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * danger of dvmIsValidObject() being called on a valid 557f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * pointer whose bit isn't set. 558f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 559f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Freeing will only happen during the sweep phase, which 560f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * only happens while the heap is locked. 561f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 5626343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return dvmHeapSourceContains(obj); 563f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 564f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return false; 565f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 566f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 567364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes/* 568364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes * Returns true iff <obj> points to a word-aligned address within Heap 569364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes * address space. 570364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes */ 571364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayesbool dvmIsValidObjectAddress(const void* ptr) 572364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes{ 573364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes /* Don't bother if it's not 4-byte aligned. 574364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes */ 575364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes if (((uintptr_t)ptr & (4-1)) == 0) { 576364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes return dvmHeapSourceContainsAddress(ptr); 577364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes } 578364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes return false; 579364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes} 580364f9d924cbd9d392744a66f80cc084c3d80caf0Barry Hayes 581f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectsize_t dvmObjectSizeInHeap(const Object *obj) 582f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 5836343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return dvmHeapSourceChunkSize(obj); 584f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 585f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 586f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 587962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes * Scan every live object in the heap, holding the locks. 588962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes */ 589962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayesstatic void verifyHeap() 590962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes{ 591962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes // TODO: check the locks. 592962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes HeapBitmap *liveBits = dvmHeapSourceGetLiveBits(); 593962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes dvmVerifyBitmap(liveBits); 594962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes} 595962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes 596962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes/* 597f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Initiate garbage collection. 598f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 599f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * NOTES: 600f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * - If we don't hold gDvm.threadListLock, it's possible for a thread to 601f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * be added to the thread list while we work. The thread should NOT 602f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * start executing, so this is only interesting when we start chasing 603f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * thread stacks. (Before we do so, grab the lock.) 604f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 605f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * We are not allowed to GC when the debugger has suspended the VM, which 606f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * is awkward because debugger requests can cause allocations. The easiest 607f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * way to enforce this is to refuse to GC on an allocation made by the 608f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * JDWP thread -- we have to expand the heap or fail. 609f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 6102954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapirovoid dvmCollectGarbageInternal(bool clearSoftRefs, GcReason reason) 611f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 612f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project GcHeap *gcHeap = gDvm.gcHeap; 61303f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro u4 suspendStart, totalTime; 61403f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro u4 rootStart, rootEnd, rootTime, rootSuspendTime; 61503f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro u4 dirtyStart, dirtyEnd, dirtyTime, dirtySuspendTime; 616f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int numFreed; 617f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t sizeFreed; 618d25566d9278e6424e521f4b7148ac31480e60c5cCarl Shapiro GcMode gcMode; 619ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro int oldThreadPriority = kInvalidPriority; 620f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 621f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* The heap lock must be held. 622f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 623f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 624f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->gcRunning) { 625f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGW_HEAP("Attempted recursive GC\n"); 626f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return; 627f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 62803f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro 629d25566d9278e6424e521f4b7148ac31480e60c5cCarl Shapiro gcMode = (reason == GC_FOR_MALLOC) ? GC_PARTIAL : GC_FULL; 630f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->gcRunning = true; 631f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 63203f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro suspendStart = dvmGetRelativeTimeMsec(); 633f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmSuspendAllThreads(SUSPEND_FOR_GC); 63403f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro rootStart = dvmGetRelativeTimeMsec(); 635f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 636ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro /* 637ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * If we are not marking concurrently raise the priority of the 638ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * thread performing the garbage collection. 639f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 640ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (reason != GC_CONCURRENT) { 641ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro /* Get the priority (the "nice" value) of the current thread. The 642ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * getpriority() call can legitimately return -1, so we have to 643ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * explicitly test errno. 644f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 645ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro errno = 0; 646ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro int priorityResult = getpriority(PRIO_PROCESS, 0); 647ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (errno != 0) { 648ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro LOGI_HEAP("getpriority(self) failed: %s\n", strerror(errno)); 649ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro } else if (priorityResult > ANDROID_PRIORITY_NORMAL) { 650ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro /* Current value is numerically greater than "normal", which 651ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * in backward UNIX terms means lower priority. 652ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro */ 653256fc159a267859c18e11e1d15fd7d97a59757c6San Mehat 654ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (priorityResult >= ANDROID_PRIORITY_BACKGROUND) { 655ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro set_sched_policy(dvmGetSysThreadId(), SP_FOREGROUND); 656ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro } 657256fc159a267859c18e11e1d15fd7d97a59757c6San Mehat 658ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL) != 0) { 659ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro LOGI_HEAP("Unable to elevate priority from %d to %d\n", 660ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro priorityResult, ANDROID_PRIORITY_NORMAL); 661ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro } else { 662ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro /* priority elevated; save value so we can restore it later */ 663ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro LOGD_HEAP("Elevating priority from %d to %d\n", 664ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro priorityResult, ANDROID_PRIORITY_NORMAL); 665ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro oldThreadPriority = priorityResult; 666ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro } 667f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 668f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 669f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 670f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Wait for the HeapWorker thread to block. 671f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * (It may also already be suspended in interp code, 672f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * in which case it's not holding heapWorkerLock.) 673f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 674f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockMutex(&gDvm.heapWorkerLock); 675f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 676f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Make sure that the HeapWorker thread hasn't become 677f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * wedged inside interp code. If it has, this call will 678f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * print a message and abort the VM. 679f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 680f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAssertHeapWorkerThreadRunning(); 681f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 682f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Lock the pendingFinalizationRefs list. 683f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 684f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Acquire the lock after suspending so the finalizer 685f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * thread can't block in the RUNNING state while 686f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we try to suspend. 687f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 688f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockMutex(&gDvm.heapWorkerListLock); 689f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 690962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes if (gDvm.preVerify) { 691962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes LOGV_HEAP("Verifying heap before GC"); 692962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes verifyHeap(); 693962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes } 694962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes 695f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 696f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmMethodTraceGCBegin(); 697f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 698f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 699f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if WITH_HPROF 700f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 701f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Set DUMP_HEAP_ON_DDMS_UPDATE to 1 to enable heap dumps 702f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * whenever DDMS requests a heap update (HPIF chunk). 703f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The output files will appear in /data/misc, which must 704f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * already exist. 705f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * You must define "WITH_HPROF := true" in your buildspec.mk 706f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * and recompile libdvm for this to work. 707f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 708f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * To enable stack traces for each allocation, define 709f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * "WITH_HPROF_STACK := true" in buildspec.mk. This option slows down 710f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * allocations and also requires 8 additional bytes per object on the 711f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * GC heap. 712f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 713f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#define DUMP_HEAP_ON_DDMS_UPDATE 0 714f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if DUMP_HEAP_ON_DDMS_UPDATE 715f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofDumpOnGc |= (gcHeap->ddmHpifWhen != 0); 716f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 717f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 718f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->hprofDumpOnGc) { 719f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project char nameBuf[128]; 720f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 72199409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project gcHeap->hprofResult = -1; 72299409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project 723f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->hprofFileName == NULL) { 724f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* no filename was provided; invent one */ 725f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project sprintf(nameBuf, "/data/misc/heap-dump-tm%d-pid%d.hprof", 726f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project (int) time(NULL), (int) getpid()); 727f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofFileName = nameBuf; 728f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 7296bf992c9d51f1e12aa37fe4c791c156402a9b79bAndy McFadden gcHeap->hprofContext = hprofStartup(gcHeap->hprofFileName, 7304b851a75f7712086a9fc4427f68c99b83725f37dAndy McFadden gcHeap->hprofFd, gcHeap->hprofDirectToDdms); 731f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->hprofContext != NULL) { 732f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project hprofStartHeapDump(gcHeap->hprofContext); 733f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 734f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofDumpOnGc = false; 735f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofFileName = NULL; 736f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 737f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 738f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 739f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Set up the marking context. 740f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 741d25566d9278e6424e521f4b7148ac31480e60c5cCarl Shapiro if (!dvmHeapBeginMarkStep(gcMode)) { 74299409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project LOGE_HEAP("dvmHeapBeginMarkStep failed; aborting\n"); 74399409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project dvmAbort(); 74499409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project } 745f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 746f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Mark the set of objects that are strongly reachable from the roots. 747f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 748f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Marking..."); 749f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapMarkRootSet(); 750f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 751f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* dvmHeapScanMarkedObjects() will build the lists of known 752f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * instances of the Reference classes. 753f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 754f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferences = NULL; 755f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->weakReferences = NULL; 756f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->phantomReferences = NULL; 757f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 758ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (reason == GC_CONCURRENT) { 759ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro /* 760ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * Resume threads while tracing from the roots. We unlock the 761ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * heap to allow mutator threads to allocate from free space. 762ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro */ 76303f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro rootEnd = dvmGetRelativeTimeMsec(); 764ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro dvmUnlockHeap(); 765ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro dvmResumeAllThreads(SUSPEND_FOR_GC); 76603f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro rootSuspendTime = rootStart - suspendStart; 76703f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro rootTime = rootEnd - rootStart; 768ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro } 769ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro 770f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Recursively mark any objects that marked objects point to strongly. 771f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * If we're not collecting soft references, soft-reachable 772f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * objects will also be marked. 773f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 774f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Recursing..."); 775f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapScanMarkedObjects(); 776f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 777ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (reason == GC_CONCURRENT) { 778ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro /* 779ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * Re-acquire the heap lock and perform the final thread 780ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * suspension. 781ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro */ 782ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro dvmLockHeap(); 78303f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro suspendStart = dvmGetRelativeTimeMsec(); 784ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro dvmSuspendAllThreads(SUSPEND_FOR_GC); 78503f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro dirtyStart = dvmGetRelativeTimeMsec(); 786ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro /* 787ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * As no barrier intercepts root updates, we conservatively 788ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * assume all roots may be gray and re-mark them. 789ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro */ 790ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro dvmHeapMarkRootSet(); 791ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro /* 792ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * Recursively mark gray objects pointed to by the roots or by 793ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro * heap objects dirtied during the concurrent mark. 794ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro */ 795ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro dvmMarkDirtyObjects(); 796ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro } 797ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro 798f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* All strongly-reachable objects have now been marked. 799f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 8002954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro LOGD_HEAP("Handling soft references..."); 8012954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro if (!clearSoftRefs) { 8022954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro dvmHandleSoftRefs(&gcHeap->softReferences); 8032954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro } 8042954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro dvmClearWhiteRefs(&gcHeap->softReferences); 8052954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro 806f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Handling weak references..."); 8072954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro dvmClearWhiteRefs(&gcHeap->weakReferences); 808f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 809f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Once all weak-reachable objects have been taken 810f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * care of, any remaining unmarked objects can be finalized. 811f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 812f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Finding finalizations..."); 813f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapScheduleFinalizations(); 814f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 8152954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro LOGD_HEAP("Handling f-reachable soft references..."); 8162954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro dvmClearWhiteRefs(&gcHeap->softReferences); 8172954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro 8182954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro LOGD_HEAP("Handling f-reachable weak references..."); 8192954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro dvmClearWhiteRefs(&gcHeap->weakReferences); 8202954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro 821f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Any remaining objects that are not pending finalization 822f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * could be phantom-reachable. This will mark any phantom-reachable 823f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * objects, as well as enqueue their references. 824f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 825f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Handling phantom references..."); 8262954074adc6a9793f38c0c58d03ec1dc057406ebCarl Shapiro dvmClearWhiteRefs(&gcHeap->phantomReferences); 827f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 828f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_DEADLOCK_PREDICTION 829f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDumpMonitorInfo("before sweep"); 830f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 831f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Sweeping..."); 832d25566d9278e6424e521f4b7148ac31480e60c5cCarl Shapiro dvmHeapSweepUnmarkedObjects(gcMode, &numFreed, &sizeFreed); 833f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_DEADLOCK_PREDICTION 834f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDumpMonitorInfo("after sweep"); 835f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 836f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 837f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Cleaning up..."); 838f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapFinishMarkStep(); 839f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 840f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Done."); 841f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 842f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Now's a good time to adjust the heap size, since 843f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we know what our utilization is. 844f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 845f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * This doesn't actually resize any memory; 846f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * it just lets the heap grow more when necessary. 847f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 848f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapSourceGrowForUtilization(); 849f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapSizeChanged(); 850f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 851f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if WITH_HPROF 852f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->hprofContext != NULL) { 853f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project hprofFinishHeapDump(gcHeap->hprofContext); 854f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: write a HEAP_SUMMARY record 85599409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project if (hprofShutdown(gcHeap->hprofContext)) 85699409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project gcHeap->hprofResult = 0; /* indicate success */ 857f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofContext = NULL; 858f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 859f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 860f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 861f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Now that we've freed up the GC heap, return any large 862f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * free chunks back to the system. They'll get paged back 863f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * in the next time they're used. Don't do it immediately, 864f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * though; if the process is still allocating a bunch of 865f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * memory, we'll be taking a ton of page faults that we don't 866f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * necessarily need to. 867f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 868f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Cancel any old scheduled trims, and schedule a new one. 869f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 870f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmScheduleHeapSourceTrim(5); // in seconds 871f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 872f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 873f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmMethodTraceGCEnd(); 874f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 875962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes LOGV_HEAP("GC finished"); 876962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes 877962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes if (gDvm.postVerify) { 878962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes LOGV_HEAP("Verifying heap after GC"); 879962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes verifyHeap(); 880962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes } 881f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 882f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->gcRunning = false; 883f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 884962adba4e5db286a36bc8024f5c023bcf6f29312Barry Hayes LOGV_HEAP("Resuming threads"); 885f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.heapWorkerListLock); 886f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.heapWorkerLock); 887f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 888c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng#if defined(WITH_JIT) 889c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng /* 890c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * Patching a chaining cell is very cheap as it only updates 4 words. It's 891c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * the overhead of stopping all threads and synchronizing the I/D cache 892c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * that makes it expensive. 893c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * 894c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * Therefore we batch those work orders in a queue and go through them 895c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * when threads are suspended for GC. 896c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng */ 897c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng dvmCompilerPerformSafePointChecks(); 898c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng#endif 899c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng 90003f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro dirtyEnd = dvmGetRelativeTimeMsec(); 90103f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro 90203f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro if (reason == GC_CONCURRENT) { 90303f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro dirtySuspendTime = dirtyStart - suspendStart; 90403f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro dirtyTime = dirtyEnd - dirtyStart; 90503f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro } 90603f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro 907f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmResumeAllThreads(SUSPEND_FOR_GC); 908ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro 909ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro if (reason == GC_CONCURRENT) { 910ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro /* 911ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * Wake-up any threads that blocked after a failed allocation 912ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro * request. 913ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro */ 914ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro dvmBroadcastCond(&gDvm.gcHeapCond); 915ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro } 916ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro 917ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (reason != GC_CONCURRENT) { 918ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (oldThreadPriority != kInvalidPriority) { 919ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (setpriority(PRIO_PROCESS, 0, oldThreadPriority) != 0) { 920ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro LOGW_HEAP("Unable to reset priority to %d: %s\n", 921ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro oldThreadPriority, strerror(errno)); 922ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro } else { 923ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro LOGD_HEAP("Reset priority to %d\n", oldThreadPriority); 924ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro } 925256fc159a267859c18e11e1d15fd7d97a59757c6San Mehat 926ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro if (oldThreadPriority >= ANDROID_PRIORITY_BACKGROUND) { 927ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro set_sched_policy(dvmGetSysThreadId(), SP_BACKGROUND); 928ec805eaed940e40212e85b58b163c7649feaca56Carl Shapiro } 929256fc159a267859c18e11e1d15fd7d97a59757c6San Mehat } 930f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 931f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 93203f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro if (reason != GC_CONCURRENT) { 93303f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro u4 suspendTime = rootStart - suspendStart; 93403f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro u4 markSweepTime = dirtyEnd - rootStart; 93503f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro totalTime = suspendTime + markSweepTime; 93603f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro LOGD("%s freed %d objects / %zd bytes in (%ums) %ums", 93703f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro GcReasonStr[reason], numFreed, sizeFreed, 93803f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro suspendTime, markSweepTime); 93903f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro } else { 94003f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro totalTime = rootSuspendTime + rootTime + dirtySuspendTime + dirtyTime; 94103f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro LOGD("%s freed %d objects / %zd bytes in (%ums) %ums (%ums) %ums", 94203f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro GcReasonStr[reason], numFreed, sizeFreed, 94303f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro rootSuspendTime, rootTime, 94403f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro dirtySuspendTime, dirtyTime); 94503f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro } 94603f3b1394cc8421d125fd00455858944f0e9808dCarl Shapiro dvmLogGcStats(numFreed, sizeFreed, totalTime); 947f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->ddmHpifWhen != 0) { 948f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Sending VM heap info to DDM\n"); 949f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDdmSendHeapInfo(gcHeap->ddmHpifWhen, false); 950f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 951f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->ddmHpsgWhen != 0) { 952f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Dumping VM heap to DDM\n"); 953f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDdmSendHeapSegments(false, false); 954f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 955f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->ddmNhsgWhen != 0) { 956f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Dumping native heap to DDM\n"); 957f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDdmSendHeapSegments(false, true); 958f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 959f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 960f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 961ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapirovoid dvmWaitForConcurrentGcToComplete(void) 962ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro{ 963ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro Thread *self = dvmThreadSelf(); 964ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro ThreadStatus oldStatus; 965ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro assert(self != NULL); 966ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); 967ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro dvmWaitCond(&gDvm.gcHeapCond, &gDvm.gcHeapLock); 968ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro dvmChangeStatus(self, oldStatus); 969ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro} 970ec47e2e081dcd43dca10d5e2c6856f73e94b0460Carl Shapiro 971f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if WITH_HPROF 972f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 973f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Perform garbage collection, writing heap information to the specified file. 974f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 9754b851a75f7712086a9fc4427f68c99b83725f37dAndy McFadden * If "fd" is >= 0, the output will be written to that file descriptor. 9764b851a75f7712086a9fc4427f68c99b83725f37dAndy McFadden * Otherwise, "fileName" is used to create an output file. 9774b851a75f7712086a9fc4427f68c99b83725f37dAndy McFadden * 978f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * If "fileName" is NULL, a suitable name will be generated automatically. 9794b851a75f7712086a9fc4427f68c99b83725f37dAndy McFadden * (TODO: remove this when the SIGUSR1 feature goes away) 9804b851a75f7712086a9fc4427f68c99b83725f37dAndy McFadden * 9814b851a75f7712086a9fc4427f68c99b83725f37dAndy McFadden * If "directToDdms" is set, the other arguments are ignored, and data is 9824b851a75f7712086a9fc4427f68c99b83725f37dAndy McFadden * sent directly to DDMS. 98399409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project * 98499409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project * Returns 0 on success, or an error code on failure. 985f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 9864b851a75f7712086a9fc4427f68c99b83725f37dAndy McFaddenint hprofDumpHeap(const char* fileName, int fd, bool directToDdms) 987f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 98899409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project int result; 98999409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project 990f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockMutex(&gDvm.gcHeapLock); 991f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 992f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.gcHeap->hprofDumpOnGc = true; 993f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.gcHeap->hprofFileName = fileName; 9944b851a75f7712086a9fc4427f68c99b83725f37dAndy McFadden gDvm.gcHeap->hprofFd = fd; 9956bf992c9d51f1e12aa37fe4c791c156402a9b79bAndy McFadden gDvm.gcHeap->hprofDirectToDdms = directToDdms; 9961b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes dvmCollectGarbageInternal(false, GC_HPROF_DUMP_HEAP); 99799409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project result = gDvm.gcHeap->hprofResult; 998f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 999f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.gcHeapLock); 100099409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project 100199409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project return result; 1002f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 1003f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1004f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmHeapSetHprofGcScanState(hprof_heap_tag_t state, u4 threadSerialNumber) 1005f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 1006f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.gcHeap->hprofContext != NULL) { 1007f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project hprofSetGcScanState(gDvm.gcHeap->hprofContext, state, 1008f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project threadSerialNumber); 1009f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 1010f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 1011f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 1012