在Android 3.0之前,Bitmap的分配是在native heap上释簿,bitmap使用完成后需要使用recycle來進(jìn)行釋放亚隅。
在Android 3.0之后,Bitmap的分配是在Java Heap上庶溶,bitmap由GC來進(jìn)行分配煮纵,因為Bitmap的回收比較耗時,所以要盡量減少在Bitmap的分配和釋放偏螺。通過BitmapFactory進(jìn)行decode生成bitmap時要盡量通過BitmapFactory.Options.inBitmap重用bitmap行疏。
BitmapFactory.cpp中用來生成bitmap的decode**方法,內(nèi)部都會調(diào)用到
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上創(chuàng)建的