Reputation: 14556
I have following code:
loadedImageDraggable.setBitmap(bitmap);
Log.v(TAG, "recycled image1 :"+bitmap);
Log.v(TAG, "recycled image2 :"+loadedImageDraggable.getBitmap());
bitmap.recycle();
bitmap = null;
Log.v(TAG, "recycled image3 :"+bitmap);
Log.v(TAG, "recycled image4 :"+loadedImageDraggable.getBitmap());
So what I expected when I coded this, is that the bitmap object will get removed from the memory. What I actually got, when I ran the code is this log trace:
recycled image1 :android.graphics.Bitmap@41afa8e0
recycled image2 :android.graphics.Bitmap@41afa8e0
recycled image3 :null
recycled image4 :android.graphics.Bitmap@41afa8e0
You can see in the last line, that there is still this bitmap object around, wrapped in the loadedImageDraggable. Since objects are passed to the methods via reference, I expected java to cleanup all references to that bitmap object when the object gets set to null. I am confused :/ Can someone clear this up for me?
Upvotes: 2
Views: 610
Reputation: 1952
Before any further explanation i would like to give you a general overview how recycle works.
Here is the definition,
public void recycle () Since: API Level 1 bitmap.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.
In your case you are still holding the references to bitmap object.. but made a call to make bitmap drawable DEAD.
When you nullify your loadedImageDraggable
by setBitmap(null). It will be eligible for GC.
Even then GC operation is dependent upon several conditions and one of them is resource hunger. Until then you never know it will be collected. Hope this will give you a better understanding.
Upvotes: 1
Reputation: 6929
You have got two reference to the same bitmap object. one is bitmap
the other is inside loadedImageDraggable
now if you set bitmap
to null the reference inside loadedImageDraggable
is of course not changed. It still points to your Bitmap@41afa8e0
Upvotes: 2