Monday, July 25, 2011

ViewFlipper and bitmaps on Android 3.0

Last about half month's time, I was working hard on the memory leak in one of our company's project.

I wasted a lot of time on figuring out what's causing the memory leaks. Finally I found a tools.

This is a very helpful tools and I should look for this solution at the very first, but I waste my time. It's called MotoDev Studio, a FREE plugin for Eclipse.

You can download it here:

Any one who used this plugin will say, this is just a integration solution for Android/Java tools. Yes, your are right man, but this make us more and more easier on Android Development.

The one I'm using for generate memory report is called "Memory Analyzer Tool(MAT)", by running this while you are debugging your android apps, will give us a full report on memory usage.

So the most important one for me is the Memory Leaks report. by using this, I found that the memory leaks are coming from two parts:


Then I start to look for solutions for these memory leaking Objects. But anyhow, I can't find any good solution for ViewFlipper. The only way I'm doing is, completely remove the ViewFlipper and use a handler to do the view switches. This is troublesome but really resolve the memory leaking.

For bitmaps, there are few things need to be care when using bitmaps:

  1. ImageView: try not to bind images by using "android:src", but using the ImageView.setImageResouce() or something like that.
  2. View Switch: if you have a lot of ImageViews using in switching, please remove all and only use one inside your code, remove the ImageView on switch to other kind of Views, set it to use another image then add a again when you need ImageView again.
  3. Use willNotCache() in ImageView.
  4. remember to set bitmap null or release it when finish using it, push it to GC.
There are still a lot of things need to be careful when using bitmaps, but in one words: remember always release the memory using for the bitmaps when it's not needed any more.

I do 3 days' work on this and successfully reduce the memory use for our app from 40MB to 8.7 MB.

No comments:

Post a Comment