In an attempt at reducing total heap size of my app I was trying to change a TableLayout into a ListView.
The source list is an ArrayList which items containing a drawble, a text and a checkbox state. This array is entirely initialized beforehand.
Before opening the activity using either the ListView or TableLayout, heap size is about 7.5Mb
With the TableLayout it grows to 8.3Mb and doesn't grow higher With the ListView it grows to 8.1Mb initially and grows to 11.5Mb when scrolling through the list!
Why is the ListView consuming more when scrolling whereas it's supposed to reuse views? I was expecting that ListView will consume less and actually not grow when scrolling!?
Is the behavior I'm seeing normal or expected or am I missing something?
I've verified the ListAdapter does reuse views, here is an extract of the ListAdapter code. It contains the only new() in the getView() method.
private static class AppListAdapter implements ListAdapter
{
WeakReference<MyActivity> wr;
ArrayList<AppInfo> app_list;
public AppListAdapter(MyActivity activity, ArrayList<AppInfo> list)
{
wr = new WeakReference<at_startup_apps>(activity);
app_list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
MyActivity activity = wr.get();
if (sa == null)
return null;
AppInfo ai = app_list.get(position);
LinearLayout ll;
ImageView img;
TextView new_txt;
CheckBox cb;
if (convertView == null)
{
ll = new LinearLayout(activity);
img = new ImageView(sa);
img.setScaleType(ScaleType.FIT_CENTER);
ll.addView(img);
new_txt = new TextView(activity);
new_txt.setOnClickListener(activity.onTextClick);
new_txt.setGravity(Gravity.CENTER_VERTICAL);
new_txt.setPadding(4, 2, 4, 2);
new_txt.setTextSize(activity.font_size + 2);
ll.addView(new_txt);
cb = new CheckBox(sa);
ll.addView(cb);
}
else
{
Log.d(at_data.TAG, "Reusing view " + position);
ll = (LinearLayout)convertView;
img = (ImageView)ll.getChildAt(0);
new_txt = (TextView)ll.getChildAt(1);
cb = (CheckBox)ll.getChildAt(2);
}
ll.setId(position);
img.setImageDrawable(ai.drawable);
new_txt.setText(ai.name);
new_txt.setId(position);
cb.setOnCheckedChangeListener(null);
if (!ai.frozen)
cb.setChecked(true);
cb.setOnCheckedChangeListener(sa.onCheckedListener);
cb.setId(position);
return ll;
}