OOM Android

OOM == OutOfMemory is one of the most annoying and frustrated issue  you can experience on the Android platform.

To avoid the issue you have to be very careful on your code, especially when you share references between different objects or activities. There is no unique solution for OOM, because the memory usage really depends on your code and how you code, what your app is doing and so on.

The first thing to do is to install a memory tracker, track allocations,  dump the heap stack and have “fun” :p

Well nevertheless there is one thing that I can do to help you, is provide you a tool method you can call on OnDestroy() event, when you are about leaving your activity.

The method is not speed optimized (mostly because of the use of the instanceof keyword and the recursive call) but does well its job: release all bitmaps of the activity! The only thing you have to do is to pass the parent view as the parameter, then it will release all the bitmaps of the parent and children views.

-recycle bmp buffer for the background’s view and/or the bitmap of the ImageView.

-set reference to null for the garbage collector

-set the drawing callback function to null

public void releaseView(View parent)
	   {
		   if(parent instanceof ViewGroup)
		   {
			   int c  = ((ViewGroup) parent).getChildCount();
			   for (int i=0;i<c;i++)
			   {
				   releaseView(((ViewGroup) parent).getChildAt(i));
			   }
			   Drawable d = parent.getBackground();
			   if(d != null)
			   {
				   if(d instanceof BitmapDrawable)
					   ((BitmapDrawable) d).getBitmap().recycle();
				   d.setCallback(null);
			   }
			   parent.setBackgroundDrawable(null);
		   }
		   else
		   {

			   if(parent instanceof ImageView)
			   {
				   Drawable d =((ImageView) parent).getDrawable();
				   if(d != null)
				   {
					   d.setCallback(null);
					   if(d instanceof BitmapDrawable)
						   if(((BitmapDrawable) d).getBitmap() != null)
							   ((BitmapDrawable) d).getBitmap().recycle();
				   }

				   d=((ImageView) parent).getBackground();
				   if(d != null)
				   {
					   	if(d instanceof BitmapDrawable)
					   		if(((BitmapDrawable) d).getBitmap() != null)
					   			((BitmapDrawable) d).getBitmap().recycle();
					   	d.setCallback(null);
				   }
				   ((ImageView) parent).setImageBitmap(null);
			   }
			   else
			   {
			       Drawable d = parent.getBackground();
				   if(d instanceof BitmapDrawable)
					   ((BitmapDrawable) d).getBitmap().recycle();
				   if(d != null)
					   d.setCallback(null);
				   parent.setBackgroundDrawable(null);
			   }
		   }
	   }

Feel free to suggest me some changes, I’m always looking forward to improve it (ImageButton for instance).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: