5

If I have:

Bitmap bitmap = Bitmap.create(..); // instance a
bitmap = Bitmap.create(...); // instance b
bitmap = null;
bitmap = Bitmap.create(...); // instance c
bitmap.recycle();
bitmap = Bitmap.create(...); // instance d
bitmap.recycle();
bitmap = null;

Once this code is executed, which of the 4 instances are still in memory? I know .recycle() instructs the native code to deallocate all resources to that object, but there's no guarantee of when that happens.

The reason I'm asking, is let's look at the following loop:

Bitmap bitmap = null;

while (true) {
    bitmap = Bitmap.create(...);    
}

This I assume will eventually crash the app (out of memory)? If so, how should this loop be rewritten? (If I'm using bitmap to animate and display changed state).

1
  • 2
    You can't know if those Bitmaps still remain in memory. I think if you call gb() after recycle() you can be sure that the last two are not in memory. Android Developers: This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. Commented Aug 18, 2012 at 15:56

2 Answers 2

1

Java (and by extension Android) use asynchronous garbage collection to clean up allocated memory. The collector runs in the background freeing what memory it can. What this means is that at no point in time can you guarantee* how many unreachable objects have been cleaned up vs. those that are still pending.

Immediately after your first code block has finished executing it's possible all four Bitmap objects still exist on the heap, but at some point the garbage collector will run, determine that they're all no longer reachable, and free up their associated memory.

The same thing is true in your second code block; some arbitrary number of objects will still exist on the heap even though they are no longer reachable, however the GC will do its best to clean them up as this happens. In practice the GC is very good at cleaning up such short-lived objects, so it may even be able to keep pace with your loop (but that isn't a guarantee).

The JVM/ART will do its best to avoid an OutOfMemory situation, including an aggressive last-ditch garbage collection to attempt to reclaim any possible memory it can to keep the application running. Therefore it's unlikely your second loop would cause any issues, but it'd be easy enough for you to test.

Hopefully you're not literally creating and immediately discarding thousands of Bitmap objects; presumably they're being used for at least some period of time during which you aren't just creating more Bitmaps that literally go unused (if so, that's your problem).

* There are tricks, such as using PhantomReference, which do let you monitor the status of garbage collection, however these will put more burden on the GC and thus aren't a good idea - or necessary - in general.

Sign up to request clarification or add additional context in comments.

Comments

0

Android Uses DalvikVM which functions almost similar to Java. It does not recollect unless required.

MAT is an excellent tool to analyze memeory dumps using hprof for android.

You can find answer yourself using MAT

2 Comments

" It does not recollect unless required." Yet my app (and MANY apps according to StackOverflow) are terminated due to Out of Memory Exceptions. So I would argue it's not doing a good job of recollecting.
I have used Bitmaps in android application and only instance it thrown Out of Memory was when I had not used recycle() properly. Which I could found out using MAT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.