大量的垃圾收集在一个列表视图 [英] Lots of garbage collection in a listview

查看:183
本文介绍了大量的垃圾收集在一个列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用自定义适配器的ListView。自定义适配器的getView使用所有推荐做法:

I have a ListView that uses a custom adapter. The custom adapter's getView uses all the recommended practices:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    SuscriptionsViewsHolder holder;
    ItemInRootList item = mItemsInList.get(position);

    if (convertView == null) {
         convertView = mInflater.inflate(R.layout.label, null);

         holder = new SuscriptionsViewsHolder();
         holder.label = (TextView) convertView.findViewById(R.id.label_label);
         holder.icon = (ImageView) convertView.findViewById(R.id.label_icon);

        convertView.setTag(holder);
    } else {
        holder = (SuscriptionsViewsHolder) convertView.getTag();
    }

    String text = String.format("%1$s (%2$s)", item.title, item.unreadCount);
    holder.label.setText(text);
    holder.icon.setImageResource(item.isLabel ? R.drawable.folder : R.drawable.file );

    return convertView;
}

然而,当我滚动,这是因为沉重的垃圾回收不畅:

However when I scroll, it is sluggish because of heavy garbage collection:

GC_EXTERNAL_ALLOC freed 87K, 48% free 2873K/5447K, external 516K/519K, paused 30ms
GC_EXTERNAL_ALLOC freed 7K, 48% free 2866K/5447K, external 1056K/1208K, paused 29ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2866K/5447K, external 1416K/1568K, paused 28ms
GC_EXTERNAL_ALLOC freed 5K, 48% free 2865K/5447K, external 1600K/1748K, paused 27ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2865K/5447K, external 1780K/1932K, paused 30ms
GC_EXTERNAL_ALLOC freed 2K, 48% free 2870K/5447K, external 1780K/1932K, paused 26ms
GC_EXTERNAL_ALLOC freed 2K, 48% free 2870K/5447K, external 1780K/1932K, paused 25ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2870K/5447K, external 1780K/1932K, paused 26ms
GC_EXTERNAL_ALLOC freed 3K, 48% free 2870K/5447K, external 1780K/1932K, paused 25ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2870K/5447K, external 1780K/1932K, paused 29ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2870K/5447K, external 1780K/1932K, paused 29ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2871K/5447K, external 1780K/1932K, paused 28ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2871K/5447K, external 1780K/1932K, paused 26ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2870K/5447K, external 1780K/1932K, paused 27ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2870K/5447K, external 1780K/1932K, paused 29ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2870K/5447K, external 1780K/1932K, paused 26ms
GC_EXTERNAL_ALLOC freed <1K, 48% free 2870K/5447K, external 1780K/1932K, paused 34ms

什么似乎是错的?

What seems to be wrong?

编辑@ 12:47格林尼治标准​​时间:

EDIT @12:47 GMT:

在实际上它是稍微比这更复杂。我的应用程序的用户界面是基于两部分。一个是屏幕的大脑,创造了意见,处理用户输入等。另一种是片段如果该设备具有的Andr​​oid 3.0,否则它是一个活动

In fact it's slightly more complicated than this. My app UI is based on 2 parts. One is the brain of a screen, creating the views, handling user input, etc. The other is a Fragment if the device has android 3.0, otherwise it's an Activity.

GC的发生在我的Nexus One 2.3.3设备,因此,使用活动。我没有我的Xoom和我一起测试用的行为片段

The GC happened on my Nexus One 2.3.3 device, so using the Activity. I don't have my Xoom with me to test the behaviour with a Fragment.

如果需要,我可以公布源,但让我试着解释一下:

I could post the source if required, but let me try to explain it :

  • RootList 是UI的大脑。它包含 :
    • 名单,其中;&GT; 将被放置在的ListView 项目
    • 是建立此列表从一个SQLite数据库的方法
    • 包含基本上只有getView方法上面粘贴一个自定义的BaseAdapter
    • RootList is the brain of the UI. It contains :
      • a List<> of items that will be placed in the ListView.
      • a method that builds this list from a SQLite db
      • a custom BaseAdapter that contains basically only the getView method pasted above
      • 在使用XML布局
      • 布局当然有id为一个ListView android.id.list
      • 在创建活动时,
      • 活动回调被转发到使用RootList实例的 RootList 类创建(构造函数,而不是的onCreate
      • 的onCreate ,我称之为 RootList 的方法,将创建的项目列表中,并设置列表数据到我的自定义类从 BaseAdapter 派生的新实例
      • uses an XML layout
      • the layout has of course a listview with id android.id.list
      • the Activity callbacks are forwarded to the RootList class using an instance of RootList created when the activity is created (constructor, not onCreate)
      • in the onCreate, I call RootList's methods that will create the list of items, and set the list data to a new instance of my custom class derived from BaseAdapter

      编辑于5月17日@下午9时36分格林尼治标准​​时间:

      EDIT on may 17th @ 9:36PM GMT:

      下面是活动的code和做的事情类。 http://pastebin.com/EgHKRr4r

      Here's the code of the Activity and the class that does the things. http://pastebin.com/EgHKRr4r

      推荐答案

      我发现这个问题。该活动我的XML布局是:

      I found the issue. My XML layout for the activity was:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
      
          <include android:id="@+id/rootlist_header" layout="@layout/pre_honeycomb_action_bar" />
      
          <ListView android:id="@android:id/list"
              android:layout_below="@id/rootlist_header"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:textColor="#444444"
              android:divider="@drawable/list_divider"
              android:dividerHeight="1px"
              android:cacheColorHint="#00000000" />
      
      </RelativeLayout>
      

      如果我删除安卓cacheColorHint =#00000000,重GC出来了,而滚动的顺利! :)

      If I remove the android:cacheColorHint="#00000000", the heavy GC is out, and the scrolling is smooth! :)

      我真的不知道为什么这个参数设置,因为我并不需要它。也许我copypasted太多,而不是真正建立我的XML布局。

      I don't really know why this parameter was set, because I don't need it. Maybe I copypasted too much instead of actually build my XML layout.

      感谢您的支持,我真的很美联社preciate你的帮助。

      THANK YOU for your support, I really appreciate your help.

      这篇关于大量的垃圾收集在一个列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆