1

If I create an object and assign it to a variable:

Obj obj1 = null;
obj1 = myFunction(params);

(here myFunction creates a complex object)

And later I reassign the variable:

obj1 = myFunction(otherparams);   

Does in that moment a memory leak occur, because I did not destroy the previous object?

Here is the real situation:

Bitmap bmp;
bmp = drawMyBitmap(3);
//... some code
bmp = drawMyBitmap(4);

Will a memory leak happen here?

Of cource, I know that I must call bmp.recycle, but I can't do it, because the real code is the following:

Bitmap bmp;
bmp = drawMyBitmap(3);
imageView.setImageBitmap(bmp);
//... some code
// if I try to do recycle here - I receive java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
// But I need to recreate bitmap every some minutes
bmp = drawMyBitmap(4);
imageView.setImageBitmap(bmp);

So, how can I recycle the bitmap and avoid memory leaks?

3
  • 2
    Why you don't create a temp variable that hold the previous Bitmap, and after the new setImageBitmap call, you recycle the temp variable? (possible because the Bitmap is not drawn anymore) Or the real code is too complex? Commented Nov 13, 2012 at 15:50
  • Please explain.... I get info every some time. After that I draw bitmap and set this bitmap to ImageView. AND I want to show this bitmap with Rotate event (as you know it recreate Activity) - so bitmap will be static. Commented Nov 13, 2012 at 19:06
  • I don't think this should have been closed. I've edited the question to try and make it more clear. Commented Nov 14, 2012 at 5:20

3 Answers 3

1

As I understand, your problem is just you can't recycle your Bitmap cause it's used. It's pretty naive, so maybe it's wrong, but do this:

imageView.setImageBitmap(bmp);
//... some code
Bitmap tmp = bmp;
bmp = drawMyBitmap(4);
imageView.setImageBitmap(bmp);
tmp.recycle(); // As it's not anymore referenced by the ImageView, you can recycle the Bitmap safely

I didn't test it. Give feedback.

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

1 Comment

Thank you! I absolutely forget about this method. Works fine.
0

In the first case, you will release the first object's reference so the garbage collector will destroy it, leaving the second one live on memory because of new reference.

In the second case, if you are setting bitmaps to ImageViews, you can not recycle them because the view will not have the bitmap to draw the image and it will throw to you a bitmap recycled exception, so you are not "leaking" to much memory keeping 2 bitmaps on memory.

Try to use bitmap options to create them to optimise your memory comsuption if you want.

1 Comment

"you will release the first object's reference" - I can't.... because I need Bitmap.release().... But I catch java.lang.IllegalArgumentException when I try it
0
 Bitmap bmp;
 bmp = drawMyBitmap(3);
 imageView.setImageBitmap(bmp);
 //... some code
 // if I try to do recycle here - I receive java.lang.IllegalArgumentException: Cannot    draw recycled bitmaps
 // But I need to recreate bitmap every some minutes
 Bitmap temp = bmp;  //try this
 bmp = drawMyBitmap(4);
 imageView.setImageBitmap(bmp);
 temp.recycle();

1 Comment

Yes it's right way - I ask about it to Astrorvald post. But in your answer temp.recycle(); must be after imageView.setImageBitmap(bmp);

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.