Rectangle 27 0

Android: Bitmap recycle() how does it work?


Display display = getWindowManager().getDefaultDisplay();
displayWidth = display.getWidth();
displayHeight = display.getHeight();
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;

@Jeremyfa It only returns the original image if you specify a width & height that is identical to the original. In that case, scaling is moot, so it might as well save some processes by skipping it and returning the original image instead. It shouldn't "break" anything though...

As of Android 4.1, the above example may break because createScaledBitmap can return in some cases the same instance as the original one. That means you have to check that original != myBitmap before recycling original.

Context is important. The question was "Also is there a better way to load large images and display them one after another recycling on the way".

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.

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

The accepted answer says "If you want to free memory ASAP you should call recycle()". Your answer says "You will need to call myBitmap.recycle()". There's a difference between "should" and "need to", and the latter is incorrect in this case.

You don't need to call recycle(), it's just a good idea if you want to free the memory right away.

Note
Rectangle 27 0

Android: Bitmap recycle() how does it work?


+1 for an interesting but very good way of describing why the memory is not available for immediate GC - nice one.

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.

Note
Rectangle 27 0

Android: Bitmap recycle() how does it work?


Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

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

Note
Rectangle 27 0

Android: Bitmap recycle() how does it work?


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.

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.

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.

Note
Rectangle 27 0

Android: Bitmap recycle() how does it work?


Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

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

Note