Archive

Posts Tagged ‘cache’

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

Categories: Tutorials Tags: , ,

ViewHolder Pattern – Caching View Efficiently

June 16, 2011 17 comments

A – Yeah! It works!

Yo, we’ve got a list view to display something:

Sample ListView

Sample ListView

This is what people like to write for list adapter:

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View v = convertView;
		if(v == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			v = inflater.inflate(R.layout.layout_list_item, null);
		}

		TextView txtName = (TextView)v.findViewById(R.id.txtName);
		TextView txtMail = (TextView)v.findViewById(R.id.txtMail);

		Contact entry = mList.get(position);

		txtName.setText(entry.getName());
		txtMail.setText(entry.getMail());

		return v;
	}

That’s great because it works with no problem and the list displays contents nicely!!!

 

B – Dig It Out

Let’s see how it works..

+ First time loaded, oh right, it’s null!  Ok, to find our TextView controls, ‘findViewById()‘ is loaded. Great, we’ve got it!

+ Second time loaded,  it must be not null, but ‘findViewById()‘ is loaded. Well, just one time, it’s fine!

+ Next time loaded, it’s absolutely not null, eh, ‘findViewById()‘ is called by default. Eh oh..

+ ……whatever time

+ xxx th time…..something is calling ‘findViewById()‘…so stupid, damn it! But it’s still running fine … _ _!

 

C – Giving birth to a child: ViewHolder

You may see the problem, too. When the list is too big, containing lots of rows, then the calling of ‘findViewById()‘ everytime will impact slightly to your application performance, even though, it’s still working fine; however, it’s quite risky since it might be dead at some unexpected point.

This is one of most common problem, it is! Hence, from somewhere, the idea of ViewHolder pattern was created to improve this current situation.

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if(v == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			v = inflater.inflate(R.layout.layout_list_item, null);
			ViewHolder holder = new ViewHolder();
			holder.txtName = (TextView)v.findViewById(R.id.txtName);
			holder.txtMail = (TextView)v.findViewById(R.id.txtMail);
			v.setTag(holder);
		}

		Contact entry = mList.get(position);
		if(entry != null) {
			ViewHolder holder = (ViewHolder)v.getTag();
			holder.txtName.setText(entry.getName());
			holder.txtMail.setText(entry.getMail());
		}
		return v;

	}

	static class ViewHolder {
		TextView txtName;
		TextView txtMail;
	}

The ViewHolder object is static, once created, it will cache all things inside. Therefore, we can avoid calling ‘findViewById()‘ on resource everytime.

 

D – Note

– Something works doesn’t mean it’s the best. Even if it’s fine, it might contain some invisible problems.

– Use ViewHolder pattern whenever possible to cache data!

 

E – Final Words

– Just something I’ve learned and wanna share to everyone.

– Feel free to comment, suggest and request; I’m here to listen to everyone’s voice 🙂

– Free to share, free to take!

 

Cheers,

Pete Houston

Use View.tag mechanism for caching view

June 16, 2011 1 comment

This is an interesting caching alternative to ViewHolder pattern.

protected Object getViewHandle(View aParentView, int aViewToFind) {
	Object v = aParentView.getTag(aViewToFind);
	if (v==null) {
		v = aParentView.findViewById(aViewToFind);
		aParentView.setTag(aViewToFind,v);
	}
	return v;
}

The sample usage:

TextView tv = (TextView)getViewHandle(itemVi<wbr>ew,R.id.some_text_view);

This code is written by: Uncle Code Monkey at Android ViewHolder discussion

The original post from his blog.

 

Cheers,

Pete Houston

Categories: Tricks & Tips Tags: , , , ,