Repeated bitmaps in OnDestroy - "attempt to use recycled bitmap" when resuming activity

I have two simple actions A and B The user starts B with A by pressing a button, and then the user returns to A by pressing the back button.

In the onDestroy() method of activity B I reuse the background images used in activity B I am trying to understand why, when activity B starts up again, I get an "attempt to use a recycled raster map". Of course, the bitmaps will be loaded again in the onCreate () method? as if they were the first time the activity was launched.

Here is my sample code:

 public class ActivityB extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(R.layout.selectionpage); } @Override public void onDestroy() { ImageView iv = (ImageView) findViewById(R.id.imageView1); ((BitmapDrawable)iv.getDrawable()).getBitmap().recycle(); LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout1); ((BitmapDrawable)ll.getBackground()).getBitmap().recycle(); super.onDestroy(); } } 

The code I use to start activity B from A

  Intent intent = new Intent(ActivityA.this, ActivityB.class); startActivity(intent); 

selectionpage.XML:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/backgroundimage"> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/selectionimage"/> </LinearLayout> 

This part may be relevant. I'm not sure. I noticed that after starting activity B, even after its destruction, I still see an instance of my activity when analyzing a heap of memory using MAT. The path to the GC roots seems to go through Java.lang.Thread and ContextImpl.

+4
source share
3 answers

You get this error because ImageView iv and LinearLayout ll still point to the processed bitmaps. You do not need to recycle yourself inside onDestroy() . Raster images will be released when the system does not need them.

+1
source

You might have a memory leak if you have an activity reference in a separate thread that has been active for longer than it should.

This can cause iv and ll from the old activity to still use bitmap images after they are reused. You can do iv.setImageDrawable (null) and ll.setBackgroundDrawable (null), but these bitmaps are created by the system and you do not need to process them.

I assume you are trying to recycle them because you have memory problems? which would be better explained by the possible aforementioned leak.

+1
source

You must do this in code. Run the Bitmap object in code and use Imageview.setImageBitmap (bitmap)

 Bitmap bitmap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.selectionpage); bitmap = new BitmapFactory.decode... yourImageView.setImageBitmap(bitmap); } @Override public void onDestroy() { super.onDestroy(); // do recycle bitmap here bitmap.recycle(); } 
-1
source

Source: https://habr.com/ru/post/1382969/


All Articles