Android: Bitmap 如何回收() ?

假设我在位图对象中加载了一个图像,如

Bitmap myBitmap = BitmapFactory.decodeFile(myFile);

现在,如果我加载另一个像

myBitmap = BitmapFactory.decodeFile(myFile2);

第一个 myBitmap 会发生什么?它得到垃圾收集或者我必须手动垃圾收集之前载入另一个位图,例如。myBitmap.recycle()

另外,有没有更好的方法来加载大图像,并显示他们一个又一个,而在回收的道路上?

102639 次浏览

The first bitmap is not garbage collected when you decode the second one. Garbage Collector will do it later whenever it decides. If you want to free memory ASAP you should call recycle() just before decoding the second bitmap.

If you want to load really big image you should resample it. Here's an example: Strange out of memory issue while loading an image to a Bitmap object.

You will need to call myBitmap.recycle() before loading the next image.

Depending on the source of your myFile (E.g. if it is something you have no control over the original size), when loading an image instead of just simply resampling some arbitrary number, you should scale the image to the display size.

if (myBitmap != null) {
myBitmap.recycle();
myBitmap = null;
}
Bitmap original = BitmapFactory.decodeFile(myFile);
myBitmap = Bitmap.createScaledBitmap(original, displayWidth, displayHeight, true);
if (original != myBitmap)
original.recycle();
original = null;

I cache the displayWidth & displayHeight in a static that I initialized at the start of my Activity.

Display display = getWindowManager().getDefaultDisplay();
displayWidth = display.getWidth();
displayHeight = display.getHeight();

Once bitmap had been loaded in memory , in fact it was made by two part data. First part include some information about bitmap , another part include information about pixels of bitmap( it is maked up by byte array). First part exisits in Java used memory, second part exisits in C++ used memory. It can use each other's memory directly. Bitmap.recycle() is used to free the memory of C++. If you only do that,the GC will collection the part of java and the memory of C is always used.

I think the problem is this: On pre-Honeycomb versions of Android, the actual raw bitmap data is not stored in VM memory but in native memory instead. This native memory is freed when the corresponding java Bitmap object is GC'd.

However, when you run out of native memory, the dalvik GC isn't triggered, so it is possible that your app uses very little of the java memory, so the dalvik GC is never invoked, yet it uses tons of native memory for bitmaps which eventually causes an OOM error.

At least that's my guess. Thankfully in Honeycomb and later, all bitmap data is stored in the VM so you shouldn't have to use recycle() at all. But for the millions of 2.3 users (fragmentation shakes fist), you should use recycle() wherever possible (a massive hassle). Or alternatively you may be able to invoke the GC instead.

Timmmm was right.

according to : http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

In addition, prior to Android 3.0 (API Level 11), the backing data of a bitmap was stored in native memory which is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash.