Note that there are some explanatory texts on larger screens.

plurals
  1. POLots of garbage collection in a listview
    text
    copied!<p>I have a ListView that uses a custom adapter. The custom adapter's getView uses all the recommended practices:</p> <pre><code>@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; } </code></pre> <p>However when I scroll, it is sluggish because of heavy garbage collection:</p> <pre><code>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 &lt;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 &lt;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 &lt;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 &lt;1K, 48% free 2870K/5447K, external 1780K/1932K, paused 29ms GC_EXTERNAL_ALLOC freed &lt;1K, 48% free 2870K/5447K, external 1780K/1932K, paused 29ms GC_EXTERNAL_ALLOC freed &lt;1K, 48% free 2871K/5447K, external 1780K/1932K, paused 28ms GC_EXTERNAL_ALLOC freed &lt;1K, 48% free 2871K/5447K, external 1780K/1932K, paused 26ms GC_EXTERNAL_ALLOC freed &lt;1K, 48% free 2870K/5447K, external 1780K/1932K, paused 27ms GC_EXTERNAL_ALLOC freed &lt;1K, 48% free 2870K/5447K, external 1780K/1932K, paused 29ms GC_EXTERNAL_ALLOC freed &lt;1K, 48% free 2870K/5447K, external 1780K/1932K, paused 26ms GC_EXTERNAL_ALLOC freed &lt;1K, 48% free 2870K/5447K, external 1780K/1932K, paused 34ms </code></pre> <p>What seems to be wrong?</p> <p>EDIT @12:47 GMT:</p> <p>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 <code>Fragment</code> if the device has android 3.0, otherwise it's an <code>Activity</code>.</p> <p>The GC happened on my Nexus One 2.3.3 device, so using the <code>Activity</code>. I don't have my Xoom with me to test the behaviour with a <code>Fragment</code>.</p> <p>I could post the source if required, but let me try to explain it :</p> <ul> <li><code>RootList</code> is the brain of the UI. It contains : <ul> <li>a <code>List&lt;&gt;</code> of items that will be placed in the <code>ListView</code>.</li> <li>a method that builds this list from a SQLite db</li> <li>a custom BaseAdapter that contains basically only the getView method pasted above</li> </ul></li> <li><code>RootListActivity</code> is a <code>ListActivity</code>, which: <ul> <li>uses an XML layout</li> <li>the layout has of course a listview with id <code>android.id.list</code></li> <li>the <code>Activity</code> callbacks are forwarded to the <code>RootList</code> class using an instance of RootList created when the activity is created (constructor, not <code>onCreate</code>)</li> <li>in the <code>onCreate</code>, I call <code>RootList</code>'s methods that will create the list of items, and set the list data to a new instance of my custom class derived from <code>BaseAdapter</code></li> </ul></li> </ul> <p>EDIT on may 17th @ 9:36PM GMT:</p> <p>Here's the code of the Activity and the class that does the things. <a href="http://pastebin.com/EgHKRr4r">http://pastebin.com/EgHKRr4r</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload