0

I'm developing an Android app with a list view that contains lots of drawables. Most of them are the same so I'm trying to use a HashMap that returns the same allocated bitmap when it's needed but still there are lots of allocations and GC actions so that the list gets stuck from time to time when scrolling.

What can I do to optimize the performance in such case?

Caching method - loadImage Using google code

mImageWorker.loadImage(pic_url, holder.pic);
holder.*some bitmap*.setImageBitmap(Utility.getBitmap(context, id); //can be one of 5 bitmaps do I use caching with a hashMap
AMM
  • 2,195
  • 2
  • 20
  • 28

3 Answers3

2

see this example this will helps you....

don't put so much process under the getView() method . this will lack the listview scroll performance if you want to do image crapping or drawable to bitmap conversition or anything .. just do all process before loading adapter.. and just assign values for each row in getView()...

kalandar
  • 793
  • 6
  • 13
  • Thanks but I think that the GC is the problem, it gets stuck only when the GC is working and it's happening when I scroll. – AMM Jun 05 '12 at 15:14
1

If you want to optimize the code, try lazy loading concept for loading images in list. Please refer: Lazy Load images on Listview in android(Beginner Level)?

Community
  • 1
  • 1
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • Thanks, I use a lazy image loader, the problem is for some drawables that exists in my project and not on the web and one of them need to be in every cell so it gets allocated again I think... – AMM Jun 05 '12 at 12:46
  • Are you using bitmaps, if yes then call the recycle() method right after usage of bitmaps. Also use inSampleSize for your bitmaps to scale them down to say 1/8th size – Shrikant Ballal Jun 05 '12 at 12:48
  • The size is 1-3 kb. And how can I know when I'm finished when using listView (with multiple adapters)? Thanks. – AMM Jun 05 '12 at 15:08
  • As soon as you set the bitmap image to some resource such as imageView.setImageBitmap(bmp); your "bmp"(the Bitmap) is no longer required, just clear the bitmap after this statement. – Shrikant Ballal Jun 06 '12 at 09:06
1

Try to use lazy loading technique using caching, you can find many tutorial on the web:

You have to see this SO thread

Also see this:

Multithreading For Performance, a tutorial by Gilles Debunne.

This is from the Android Developers Blog. The suggested code uses:

  1. AsyncTasks.
  2. A hard, limited size, FIFO cache.
  3. A soft, easily garbage collected cache.
  4. A placeholder Drawable while you download.
Community
  • 1
  • 1
K_Anas
  • 31,226
  • 9
  • 68
  • 81