This is a followup to my earlier question ( Android memory leak? ) after doing some research and following some of the steps suggested.
My application has a UI loop in which a user clicks a ListView item, which draws another ListView after querying a web api. Clicking on an item will draw up another ListView. This process can be repeated many times by a user without finishing the activity.
Here is a code snippet of how I am populating a ListView with SimpleAdapter..
//* Add available times value using array.length()
List<Map<String,Object>> datalist = new ArrayList<Map<String,Object>>();
for (SomeResult result : results) {
Map map = new HashMap();
map.put("field_a", result.field_a);
map.put("field_b", result.field_b);
datalist.add(map);
}
String[] from = {"field_a","field_b"};
int[] to = {R.id.textview_a,R.id.textview_b};
// get a reference to the ListView
ListView lv = (ListView)findViewById(R.id.listview);
lv.setBackgroundColor(0xffffffff);
lv.setOnItemClickListener(itemclick_drawHours);
SimpleAdapter adapter = new SimpleAdapter(this.getApplicationContext(), datalist, R.layout.list_item, from, to);
lv.setAdapter(adapter);
lv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
lv.setCacheColorHint(0xffffffff);
viewFlipper.setDisplayedChild(2);
Using DDMS and MAT, I noticed that while the number of ListView objects does not increase, the number of TextViews, RelativeLayouts and ImageViews were increasing steadily along with the number of times a user has clicked and a new list has been drawn. Here are the object counts of these three widgets +ListView at various stages of app usage.
App Installed onto emulator:
android.widget.TextView: 16
android.widget.RelativeLayout: 9
android.widget.ImageView: 8
android.widget.ListView: 4
Light Use (a dozen or so clicks and ListViews drawn):
android.widget.TextView: 107
android.widget.RelativeLayout: 48
android.widget.ImageView: 34
android.widget.ListView: 4
Medium Use (a couple dozen clicks/ListViews drawn):
// At this point, a lot of GC messages and Resizing JIT Table messages etc. in LogCat
android.widget.TextView: 158
android.widget.RelativeLayout: 72
android.widget.ImageView: 61
android.widget.ListView: 4
Heavy Use (three dozen or so clicks/ListViews drawn):
// Even more messages in logcat
android.widget.TextView: 222
android.widget.RelativeLayout: 103
android.widget.ImageView: 92
android.widget.ListView: 4
The app does not actually crash while open, just gets less responsive and laggy. After a period of medium/heavy use, closing and opening the app causes an OutOfMemory Error when displaying an ImageView.
Here is list_item.xml ..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview_a"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dp"
android:textColor="#000000"
android:layout_alignParentLeft="true"
android:textSize="18dp" >
</TextView>
<ImageView
android:id="@+id/rightarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:src="@drawable/garrow"
android:layout_alignParentRight="true"
/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="" android:id="@+id/textview_b" android:visibility="gone" />
</RelativeLayout>
If you look at the 2(TextViews) to 1(ImageView) to 1(RelativeLayout) ratio in this template and in the object counts above, it seems that ImageViews, TextViews and RelativeLayouts created by SimpleAdapter are not being released properly.
Is there anything obvious that I'm missing? I've read some other posts where they say this problem only seems to occur in "debug" mode. I'm not sure how to turn this on/off except changing the android:debuggable="true" or "false" in the manifest.