美文网首页
Android Bitmap内存的分配

Android Bitmap内存的分配

作者: yangweigbh | 来源:发表于2016-12-12 16:34 被阅读145次

在Android 3.0之前,Bitmap的分配是在native heap上,bitmap使用完成后需要使用recycle来进行释放。

在Android 3.0之后,Bitmap的分配是在Java Heap上,bitmap由GC来进行分配,因为Bitmap的回收比较耗时,所以要尽量减少在Bitmap的分配和释放。通过BitmapFactory进行decode生成bitmap时要尽量通过BitmapFactory.Options.inBitmap重用bitmap。

BitmapFactory.cpp中用来生成bitmap的decode**方法,内部都会调用到

jobject GraphicsJNI::createBitmap(JNIEnv* env, android::Bitmap* bitmap,
        int bitmapCreateFlags, jbyteArray ninePatchChunk, jobject ninePatchInsets,
        int density) {
    bool isMutable = bitmapCreateFlags & kBitmapCreateFlag_Mutable;
    bool isPremultiplied = bitmapCreateFlags & kBitmapCreateFlag_Premultiplied;
    // The caller needs to have already set the alpha type properly, so the
    // native SkBitmap stays in sync with the Java Bitmap.
    assert_premultiplied(bitmap->info(), isPremultiplied);

    jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID,
            reinterpret_cast<jlong>(bitmap), bitmap->javaByteArray(),
            bitmap->width(), bitmap->height(), density, isMutable, isPremultiplied,
            ninePatchChunk, ninePatchInsets);
    hasException(env); // For the side effect of logging.
    return obj;
}

可以看到Bitmap是在Java Heap上创建的

相关文章

网友评论

      本文标题:Android Bitmap内存的分配

      本文链接:https://www.haomeiwen.com/subject/ohwmmttx.html