Archive

Posts Tagged ‘memory’

New Cache for Better Memory Performance


Hanging around and reading Android articles, and suddenly I want to check out new information on Android Training Page.

As a developer having lots of interest in Android Performance & Optimization, I check this section first: Cache with Bitmaps.

Got a note from the page:

Note: In the past, a popular memory cache implementation was a SoftReference or WeakReference bitmap cache, however this is not recommended. Starting from Android 2.3 (API Level 9) the garbage collector is more aggressive with collecting soft/weak references which makes them fairly ineffective. In addition, prior to Android 3.0 (API Level 11), the backing data of a bitmap was stored in native memory which is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash.

Just like something light up in my head, this explains why sometimes some of my apps crash in memory limitation strangely before.

The two new class introduced to work around with Bitmap Caching is: LruCache and DiskLruCache.

So if you got any problem with bitmap memory, this could be a hint to your optimizing solution for your Android apps.

 

Cheers,

Pete Houston

Advertisements
Categories: Tutorials Tags: , ,

Calculate memory usage at runtime

November 13, 2011 Leave a comment

If you’re wondering how to calculate memory usage, this tip is what you need

// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
long memoryUsed = runtime.totalMemory() - runtime.freeMemory();

Cheers,
Pete Houston

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

Categories: Tutorials Tags: , , ,

Copy Bitmap object to avoid references

September 10, 2011 2 comments

If you assign value of a Bitmap to the other one, it means you have create a reference to the source Bitmap. Hence, if either one is recycled, the other one certainly reference to the already-recycled Bitmap object in memory, which might cause some problems like: Exception on Bitmap, throwIfRecycled.

In some case, you probably want to copy a Bitmap object to a new one, here the trick:

Bitmap src = .....<allocated the source Bitmap>
Bitmap newBitmap = src.copy(src.getConfig(), src.isMutable() ? true : false);

Pretty much simple!
@Remember to recycle each one after used.

Cheers,
Pete Houston

Bitmap and memory leak! – Part 2

September 10, 2011 3 comments

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: , , , ,

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: , , , ,