在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上创建的