Android的内存泄漏与画廊内的ListView [英] Android memory leak with ListView inside a Gallery

查看:455
本文介绍了Android的内存泄漏与画廊内的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在跟踪潜在的内存泄漏在我的Andr​​oid应用程序,我遇到一个我不知道做什么用。首先,我将描述我想要做的。

I've been tracking down potential memory leaks in my Android application, and I've come across one that I'm not sure what to do with. First, I'll describe what I'm trying to do.

有关它的价值,我为建设运行埃克莱尔姜饼上的HTC难以置信(2.1,API 7级)和测试(2.3.7,API等级10)。从观察LogCat中,我想我的应用程序有大约32MB的最大堆大小。

For what it's worth, I'm building for Eclair (2.1, API level 7) and testing on an HTC Incredible running Gingerbread (2.3.7, API level 10). From observing LogCat, I assume my application has a maximum heap size of about 32MB.

我想建立,其中有接触的多个页面的地址簿。您可以通过左右滚动页面之间导航,并且通过当前浏览的页面和上下滚动浏览。要做到这一点,我使用的是图库,其适配器适应联系人列表到的ListView ,其反过来适应适配器一个联系人成 RelativeLayout的

I am trying to build an address book wherein you have several pages of contacts. You navigate between pages by scrolling left and right, and you navigate through the current page by scrolling up and down. To accomplish this, I am using a Gallery whose adapter adapts a contact list into a ListView, whose adapter in turn adapts a single contact into a RelativeLayout.

似乎一切都工作正常,但我通过画廊刷卡时耗尽本机(外置)存储的真正的迅速。我周围的图库一段时间后,刷卡作出了HPROF转储和把它放到垫子。在直方图中,我发现我有这是由我保持接触到纯粹我的联系 RelativeLayout的 s的几百的ListView 秒。下面是我发现,当我看着[截断] MAT merge_shortest_paths输出的ListView

Everything seems to be working fine, but I'm running out of native (external) memory really quickly when swiping through the gallery. I made an HPROF dump after swiping around the Gallery for awhile and pulled it into MAT. In the Histogram, I found that I had a few hundred of my contact RelativeLayouts which were held onto solely by my contact ListViews. Here's what I found when I looked at the [truncated] MAT merge_shortest_paths output of that ListView:

android.view.ViewRoot$1
+ this$0 android.view.ViewRoot
  + mAttachInfo android.view.View$AttachInfo
    + mScrollContainers java.util.ArrayList
      + array java.lang.Object[303]
        + [110], [112], [114], [116], [118], ... com.example.LeakyListView
        + ...and so forth.

抓着那些泄露的ListView S为这个 android.view.View $ AttachInfo 的<唯一code> mScrollContainers 字段。问题是,我不知道我的意见是如何获得在那里摆在首位,所以我就如何堵塞这个泄漏的损失。

The only thing holding onto those leaked ListViews was this android.view.View$AttachInfo's mScrollContainers field. Problem is, I don't know how my views are getting in there in the first place, so I'm at a loss on how to plug this leak.

我要如何解决这个内存泄露?或最起码,这是怎么定链得到建立,什么是的ViewRoot AttachInfo mScrollContainers

How do I solve this memory leak? Or at the very least, how did this reference chain get built and what is ViewRoot, AttachInfo, and mScrollContainers?

我会尽力在这里很快就隔离到一个简单的测试案例和后code,但我希望这是足以让谈话开始了。

I'll try and isolate it to a simple test case and post code here soon, but I'm hoping this is enough to get the conversation started.

推荐答案

您真的,真的应该使用ViewPager代替图库这种事情:的http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

You really, really should use ViewPager instead of Gallery for this kind of thing: http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

这篇关于Android的内存泄漏与画廊内的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆