简体   繁体   English

具有SimpleAdapter的Android ListView泄漏内存

[英]Android ListView with SimpleAdapter leaking memory

This is a followup to my earlier question ( Android memory leak? ) after doing some research and following some of the steps suggested. 在进行了一些研究并遵循了建议的一些步骤之后,这是我先前的问题( Android内存泄漏? )的后续操作。

My application has a UI loop in which a user clicks a ListView item, which draws another ListView after querying a web api. 我的应用程序有一个UI循环,用户单击其中的ListView项,在查询Web api后将绘制另一个ListView。 Clicking on an item will draw up another ListView. 单击一个项目将绘制另一个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.. 这是我如何使用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);

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. 使用DDMS和MAT,我注意到虽然ListView对象的数量没有增加,但是TextViews,RelativeLayouts和ImageViews的数量随着用户单击并绘制新列表的次数稳定增加。 Here are the object counts of these three widgets +ListView at various stages of app usage. 以下是这三个小部件+ 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

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. 经过一段时间的中/重度使用后,关闭和打开应用程序会在显示ImageView时导致OutOfMemory错误。

Here is list_item.xml .. 这是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. 如果您查看此模板中2(TextViews)对1(ImageView)对1(RelativeLayout)的比率以及上面的对象计数,则似乎没有正确释放SimpleAdapter创建的ImageViews,TextViews和RelativeLayouts。

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. 除了更改清单中的android:debuggable =“ true”或“ false”外,我不确定如何打开/关闭此功能。

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

What is result? 结果如何? sounds maybe like it keys on - time like time of event - then you are adding to a Collection keyed on a String literal -- need to read some more docs .. 听起来好像它像事件时间一样准时键入-然后您要添加一个以String文字为键的Collection-需要阅读更多文档。

list_item.xml initializes the program = it is not a coding context for the program list_item.xml初始化程序=它不是程序的编码上下文

I am pretty new at Android but I have been writing Java well enough to see you need to populate the ListView from the Collection, which without details might be 我在Android上还很陌生,但是我一直在编写Java足够好,足以看到您需要从Collection中填充ListView,而没有详细信息可能是

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

to get the new item where the program can find it you put it in collection of your choice -- they have a sparse collection datatype somewhere in the work 要在程序可以找到的位置获取新项目,您可以将其放入您选择的集合中-他们在工作中的某处具有稀疏的集合数据类型

I dont see where the leak is coming from but I bet it is from the R. as reading how that works makes that the likely candidate in my mind based on the way you are using it compared to how it works 我看不到泄漏的来源,但我敢打赌,它来自R。阅读泄漏的工作原理后,我会根据您使用它的方式(而不是泄漏的工作方式)来确定可能的候选对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM