简体   繁体   中英

Android Listview GC_FOR_ALLOC freed: DDMS android.graphics.Bitmap

I'm having trouble with a listview in android. When I start scrolling down my List, it is very slow and I see that the GC is called. When I'm at the bottom of my List, everything works fine and smooth. I think that at this point my ViewHolder does the work.

But I can't find the source that is calling the GC. I searched which lead to:

DDMS     436816    byte[]   1   android.graphics.Bitmap    nativeCreate 

I can't interpret that line. My ArrayAdapter and it's getView method looks like this:

 public class DiagnoseAdapter extends ArrayAdapter<Visualizer> {
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {

    int type = TYPE_DEFAULT;
    final Visualizer item = getItem(position);

    switch(item.getType()){
         case TYPE_DEFAULT:
                convertView = DefaultTextView.getView(position, convertView, mlayoutInflater, item, parent);
            break;

     // more cases/types

    }
  return convertView;
  }
}

which is calling the following getView Method of the class DefaultTextView

public class DefaultTextView{

 public static View getView(int position, View convertView, LayoutInflater layoutInflater, Visualizer item, ViewGroup parent){
    ViewHolder holder;
    if (convertView == null || item.getReleatedObject() == null || convertView.getTag()!=TAG_DEFAULT) {  

     convertView = layoutInflater.inflate(R.layout.diagnose_item, null);

     holder = new ViewHolder();

     holder.value = (TextView) convertView.findViewById(R.id.diagnose_function_value);
     holder.name = (TextView) convertView.findViewById(R.id.diagnose_function_setname);
     holder.mLinLayout = (LinearLayout) convertView.findViewById(R.id.default_linlayout);

     convertView.setTag(TAG_DEFAULT);
     convertView.setTag(R.layout.diagnose_item,holder);
     item.setReleatedObject(convertView);

    } else {

    holder = (ViewHolder) convertView.getTag(R.layout.diagnose_item);

    }

    holder.value.setText(item.toString());
    holder.name.setText(item.getToolTip());


        holder.mLinLayout.removeAllViews();
         if (item.getUpdateFlag(4)) {
             if (holder.back == null){
                holder.back = new ImageView(convertView.getContext());
                holder.back.setScaleType(ScaleType.FIT_CENTER);
                holder.back.setAdjustViewBounds(true);
                holder.back.setImageBitmap(bm1);
             }
             holder.mLinLayout.addView(holder.back);
         } 

        if (item.getUpdateFlag(1)) {
             if (holder.update == null){
                    holder.update = new ImageView(convertView.getContext());
                    holder.update.setScaleType(ScaleType.FIT_CENTER);
                    holder.update.setAdjustViewBounds(true);
                    holder.update.setImageBitmap(bm2);

                 }
             holder.mLinLayout.addView(holder.update);
        } 

        if (item.getUpdateFlag(2)) {
            if (holder.timer == null){
                holder.timer = new ImageView(convertView.getContext());
                holder.timer.setScaleType(ScaleType.FIT_CENTER);
                holder.timer.setAdjustViewBounds(true);
                holder.timer.setImageBitmap(bm3)

             }
            holder.mLinLayout.addView(holder.timer);
        } 

        if (item.getUpdateFlag(3)) {
            if (holder.log == null){
                holder.log = new ImageView(convertView.getContext());
                holder.log.setScaleType(ScaleType.FIT_CENTER);
                holder.log.setAdjustViewBounds(true);
                holder.log.setImageBitmap(bm4);

             }
            holder.mLinLayout.addView(holder.log);
        }

        if (item.getUpdateFlag(0)) {
            if (holder.forward == null){
                holder.forward = new ImageView(convertView.getContext());
                holder.forward.setScaleType(ScaleType.FIT_CENTER);
                holder.forward.setAdjustViewBounds(true);
                holder.forward.setImageBitmap(bm5);

             }
            holder.mLinLayout.addView(holder.forward);
        }

    return convertView;
}

static class ViewHolder {            
    TextView name, value;            
    ImageView back, update, timer, log, forward;
    LinearLayout mLinLayout;
} 

}

Even if I comment the LinearLayout out, so I just have a List with two TextViews .

So my Question. Do I miss anything. Some stupid thing? How do I get my ListView smoother? BTW: I read in a different thread, that it is happening if the ListView has the attribute android:cacheColorHint="#00000000 . I don't have this attribute.

I hope anyone has a solution. Thanks!

About the source of GC calls. If I'm understanding your code correctly, everytime your ListView items are recycled and you call removeAllViews() , a previously dynamically created ImageView is removed and its Bitmap is garbage collected. So, Maybe those GC calls would be avoided if you use the same ImageView declaring it in your xml layout and just replace the Bitmap according to your getUpdateFlag() .

And two more things about ListViews and Images. First thing is that if the image is too big, your ListView is going to be laggy no matter what. You would need to scale the image down if that is the case( Loading Large Bitmaps Efficiently ). And second, maybe you would also need to implement a Lazy List, which loads images on demand, there is a famous question about that --> How do I do a lazy load of images in ListView?

I've finally solved my Problem. Like above, I thought the problem was based on the images of my list items. But that wasn't the problem. I just didn't use my ViewHolders and the getItemViewType(int position) method correctly. I have a list with many different item layouts and I saw, that my code above created a new convertView and a new ViewHolder for every single item, which wasn't supposed to be. I found a great tutorial about how to use multiple item layouts (see link below):

Multiple List Item Layouts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM