Archive

Posts Tagged ‘leak’

Bitmap and memory leak! – Part 3

September 11, 2011 1 comment

After my previous post on Bitmap and memory leak! – Part 2, some buddies ask me how to free memory of Bitmap on a HashMap storage.

Pretty much simple:

package pete.android.study;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import android.graphics.Bitmap;

public class BitmapStorage {

	HashMap<Integer, Bitmap> mStorage;

	// constructor to create the objects
	public BitmapStorage(HashMap<Integer, Bitmap> storage) {
		mStorage = storage;

	}

	// call in order to free memory of Bitmap objects
	public void clear() {
		// get HashMap entry iterator
		Iterator<Entry<Integer, Bitmap>> iter = mStorage.entrySet().iterator();
		while(iter.hasNext()) {
			// get entry pair
			Entry<Integer, Bitmap> pair = iter.next();
			// get Bitmap object
			Bitmap image = pair.getValue();
			// recycle if...
			if(image != null && !image.isRecycled()) {
				image.recycle();
				image = null;
			}
		}

		mStorage.clear();
	}

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

}

Do the same for even complex data structure in HashMap implementation.

package pete.android.study;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import android.graphics.Bitmap;

public class BitmapStorage {

	// even a complex data structure containing Bitmap objects
	private class CustomDataHolder {
		String content;
		Bitmap image;
	}

	HashMap<Integer, CustomDataHolder> mStorage;

	// constructor to create the objects
	public BitmapStorage(HashMap<Integer, CustomDataHolder> storage) {
		mStorage = storage;

	}

	// call in order to free memory of Bitmap objects
	public void clear() {
		// get HashMap entry iterator
		Iterator<Entry<Integer, CustomDataHolder>> iter = mStorage.entrySet().iterator();
		while(iter.hasNext()) {
			// get entry pair
			Entry<Integer, CustomDataHolder> pair = iter.next();
			// get Bitmap object
			Bitmap image = pair.getValue().image;
			// recycle if...
			if(image != null && !image.isRecycled()) {
				image.recycle();
				image = null;
			}
		}

		mStorage.clear();
	}

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

}

 

Cheers,
Pete Houston

Advertisements
Categories: Tutorials Tags: , , ,

Bitmap and memory leak!

September 7, 2011 1 comment

Whenever using a Bitmap object, it consumes the heap memory! So if your code simply just declares and use it without release its memory, you would face memory leak problem.

It’s men

Remember to always free Bitmap objects memory before finishing your application or whatever objects containers.


Bitmap image = ..... (whatever)

...

if(image != null) {

image.recycle();

image = null;

}

It’s mentioned here on Android Documentation References: Bitmap.recycle()

 

Cheers,

Pete Houston

Categories: Tutorials Tags: , , , ,