Android memory leak with ListView inside a Gallery

你说的曾经没有我的故事 提交于 2020-01-05 04:01:10

问题


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.

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.

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.

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.

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.

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?

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.


回答1:


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



来源:https://stackoverflow.com/questions/8075512/android-memory-leak-with-listview-inside-a-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!