Home > Tutorials > Bitmap and memory leak! – Part 2

Bitmap and memory leak! – Part 2


One of the common mistakes developers often make is that never take care of Bitmap object stored in a data structure like List, LinkedList, HashMap

Here a sample:

public class BitmapStorage {
	String mContent;
	ArrayList<Bitmap> mImages;

	// constructor to create the objects
	public BitmapStorage(String content, ArrayList<Bitmap> images) {
		mContent = content;
		mImages = images; // references
	}
}

Things seem to be fined IF developers try to release memory of this BitmapStorage object somewhere else. WHAT IF they FORGET? It’s gonna be really a mess, isn’t it?

In my opinion, it is much better if providing such a way to handle memory in such a class.

package pete.android.study;

import java.util.ArrayList;

import android.graphics.Bitmap;

public class BitmapStorage {
	String mContent;
	ArrayList<Bitmap> mImages;

	// constructor to create the objects
	public BitmapStorage(String content, ArrayList<Bitmap> images) {
		mContent = content;
		mImages = images; // references
	}

	// call in order to free memory of Bitmap objects
	public void freeBitmaps() {
		for(Bitmap image: mImages) {
			// also, it's better to check whether it is recycled or not
			if(image != null && !image.isRecycled()) {
				image.recycle();
				image = null; // null reference
			}
		}
	}

	// the destructor should also handle the memory in case it still holds memory
	protected void finalize() {
		freeBitmaps();
	}
}

Like this, developers can freely choose to release memory whenever they feel done with the BitmapStorage objects and when they forget to do so, the destructor “finalize()” would handle the rest.

@Just a note, make sure your Bitmap objects don’t link to any other things when recycling; it would be troublesome. Try to create a nice Bitmap objects live well in a life-cycle!

 

Cheers,
Pete Houston

Categories: Tutorials Tags: , , , ,
  1. September 22, 2013 at 12:09 am

    This being to such an extent that she almost regretted that
    she was on her way home. So make sure to brush your teeth before even
    thinking about oral sex. Most women need to be turned on
    for a little while before direct clitoral stimulation can be
    pleasurable.

  2. May 23, 2013 at 12:38 pm

    whoah this blog is excellent i really like studying your articles.
    Keep up the great work! You realize, a lot of individuals are looking round for this information, you can aid them
    greatly.

  3. May 3, 2013 at 4:51 am

    Thanks for the auspicious writeup. It actually was a enjoyment account it.

    Look complex to far introduced agreeable from you!
    However, how could we keep in touch?

  1. No trackbacks yet.

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: