heap alloc

/* Try as hard as possible to allocate some memory.

 */

static void *tryMalloc(size_t size)

{

#ifdef HAVE_ANDROID_OS

    char prop_value[PROPERTY_VALUE_MAX] = {'\0'};

#endif

    char* hprof_file = NULL;

    void *ptr;

    int result = -1;

    int debug_oom = 0;

//TODO: figure out better heuristics

//    There will be a lot of churn if someone allocates a bunch of

//    big objects in a row, and we hit the frag case each time.

//    A full GC for each.

//    Maybe we grow the heap in bigger leaps

//    Maybe we skip the GC if the size is large and we did one recently

//      (number of allocations ago) (watch for thread effects)

//    DeflateTest allocs a bunch of ~128k buffers w/in 0-5 allocs of each other

//      (or, at least, there are only 0-5 objects swept each time)

                // 一次分配太大的内存会导致内存的浪费

                // 所以将大内存分开来分配

 

    ptr = dvmHeapSourceAlloc(size);

    if (ptr != NULL) {

        return ptr;

    }

    /*

     * The allocation failed.  If the GC is running, block until it

     * completes and retry.

     */

    if (gDvm.gcHeap->gcRunning) {

        /*

         * The GC is concurrently tracing the heap.  Release the heap

         * lock, wait for the GC to complete, and retrying allocating.

         */

        dvmWaitForConcurrentGcToComplete();

    } else {

        /*

         * Try a foreground GC since a concurrent GC is not currently running.

         */

        if (dvmIsGcPaused()) {

            dvmHeapSourceGrowForUtilization();

        } else {

            gcForMalloc(false);

        }

    }

 

    ptr = dvmHeapSourceAlloc(size);

    if (ptr != NULL) {

        return ptr;

    }

 

    /* Even that didn't work;  this is an exceptional state.

     * Try harder, growing the heap if necessary.

     */

    ptr = dvmHeapSourceAllocAndGrow(size);

    if (ptr != NULL) {

        size_t newHeapSize;

 

        newHeapSize = dvmHeapSourceGetIdealFootprint();

//TODO: may want to grow a little bit more so that the amount of free

//      space is equal to the old free space + the utilization slop for

//      the new allocation.

        LOGI_HEAP("Grow heap (frag case) to "

                "%zu.%03zuMB for %zu-byte allocation",

                FRACTIONAL_MB(newHeapSize), size);

        return ptr;

    }

    /* Most allocations should have succeeded by now, so the heap

     * is really full, really fragmented, or the requested size is

     * really big.  Do another GC, collecting SoftReferences this

     * time.  The VM spec requires that all SoftReferences have

     * been collected and cleared before throwing an OOME.

     */

//TODO: wait for the finalizers from the previous GC to finish

    LOGI_HEAP("Forcing collection of SoftReferences for %zu-byte allocation",

            size);

    gcForMalloc(true);

    ptr = dvmHeapSourceAllocAndGrow(size);

    if (ptr != NULL) {

        return ptr;

    }

//TODO: maybe wait for finalizers and try one last time

 

    LOGE_HEAP("Out of memory on a %zd-byte allocation.", size);

//TODO: tell the HeapSource to dump its state

    dvmDumpThread(dvmThreadSelf(), false);

    return NULL;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值