1

这是我之前的问题(Android 内存泄漏?)在进行一些研究并遵循一些建议的步骤之后的后续。

我的应用程序有一个 UI 循环,其中用户单击一个 ListView 项目,该项目在查询 Web api 后绘制另一个 ListView。单击一个项目将绘制另一个 ListView。用户可以多次重复此过程而无需完成活动。

这是我如何使用 SimpleAdapter 填充 ListView 的代码片段。

//* 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);

使用 DDMS 和 MAT,我注意到虽然 ListView 对象的数量没有增加,但 TextViews、RelativeLayouts 和 ImageViews 的数量随着用户点击和绘制新列表的次数而稳步增加。以下是这三个小部件 +ListView 在应用使用不同阶段的对象数。

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

该应用程序在打开时实际上并没有崩溃,只是响应速度变慢了。在中度/重度使用一段时间后,关闭和打开应用程序会在显示 ImageView 时导致 OutOfMemory 错误。

这是 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>

如果您查看此模板和上面的对象计数中的 2(TextViews) 与 1(ImageView) 与 1(RelativeLayout) 的比率,似乎 SimpleAdapter 创建的 ImageViews、TextViews 和 RelativeLayouts 没有正确释放。

有什么明显的我失踪了吗?我读过其他一些帖子,他们说这个问题似乎只发生在“调试”模式下。除了更改清单中的 android:debuggable="true" 或 "false" 之外,我不确定如何打开/关闭此功能。

4

1 回答 1

-1
List<Map<String,Object>> ?...List of Maps each of which keys on a string to an object ... 

结果是什么?听起来它可能像事件时间一样按时间键-然后您要添加到以字符串文字为键的集合中-需要阅读更多文档..

list_item.xml 初始化程序 = 它不是程序的编码上下文

我是 Android 的新手,但我编写 Java 已经足够好,可以看到您需要从 Collection 中填充 ListView,没有详细信息可能是

TreeMap<Integer,SomeResult>Stuff=new TreeMap<Integer,SomeResult>();

要获得程序可以找到的新项目,请将其放入您选择的集合中——它们在工作中的某处有一个稀疏集合数据类型

我没有看到泄漏的来源,但我敢打赌它来自 R。因为阅读它是如何工作的,根据你使用它的方式与它的工作方式相比,我认为它可能是我脑海中的候选者

于 2011-11-19T03:52:06.270 回答