Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If set myBitmap = null without recycle(), what difference between with recycle() [duplicate]

Tags:

android

bitmap

I see the two lines are always together:

myBitmap.recycle();
myBitmap = null;

If I only use:

myBitmap = null;

What difference?

Thanks.

like image 548
user1914692 Avatar asked Jun 28 '13 22:06

user1914692


1 Answers

According to the documentation:

public void recycle()

Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

So basically,

myBitmap = null;

Removes that specific reference to the bitmap it points to. If it's the only reference, that bitmap will be cleaned up by the garbage collector.

However,

myBitmap.recycle();
myBitmap = null;

Removes a hidden reference to the pixel data for that bitmap. It then removes your specific reference to the bitmap. So both will be garbage collected. Unless you've got a huge bitmap, or for some reason have limited memory, you probably don't need to worry about calling myBitmap.recycle().

like image 140
Shaquil Hansford Avatar answered Nov 20 '22 15:11

Shaquil Hansford