android - 向 Gallery 添加一个简单的 ScrollView 会导致内存泄漏

标签 android memory scrollview

我在使用 Gallery 组件时遇到了只能归类为 ScrollView 元素的内存泄漏。

简短的背景。我有一个现有的应用程序,它是一个照片幻灯片应用程序。 它使用 Gallery 组件,但适配器中的每个元素都以全屏显示。 (完整源代码可在 this link 获得)

适配器 View 元素由一个 ImageView 和两个用于标题和描述的 TextView 组成。 由于照片的分辨率相当高,该应用程序使用了大量内存,但画廊通常能够很好地回收它们。

但是,当我现在为描述 TextView 实现 ScrollView 时,我几乎立即遇到了内存问题。这是我做的唯一改变

<ScrollView
android:id="@+id/description_scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
  <TextView
    android:id="@+id/slideshow_description"
    android:textSize="@dimen/description_font_size"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:layout_below="@id/slideshow_title"
    android:singleLine="false"
    android:maxLines="4"/>  
</ScrollView> 

我做了一个堆转储,可以清楚地看到这是内存问题的根源所在的 Scrollview。

以下是堆转储分析的两个屏幕截图。请注意,ScrollView 保留了对 mParent 的引用,其中包括我使用的大照片 Heap analysis - leak candidate Heap analysis - drilldown to a single ScrollView

如果我使用 TextView 的滚动(android:scrollbars = "vertical"and .setMovementMethod(new ScrollingMovementMethod());

PSS 尝试关闭持久绘图缓存,但没有不同 dreaandroid:persistentDrawingCache="none"

最佳答案

您是否尝试过在容器 View 滚出屏幕时移除 ScrollView ?我不确定这是否适合您,但值得一试吗?或者,尝试在 ScrollView 离开屏幕时调用 setScrollContainer(false)。这似乎从 mScrollContainers 集中删除了 View 。

另外,this question ,由 Dianne Hackborn(android 工程师)回答,明确声明不要在画廊内使用可 ScrollView 。也许这个问题是为什么?

关于android - 向 Gallery 添加一个简单的 ScrollView 会导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8900212/

相关文章:

android - 如何为没有 3g 功能的 android 平板电脑连接 3g usb 调制解调器

java - Android 无法序列化错误

php - 具有三个for循环的php中的内存泄漏

linux - 将变量存储在任何地方(交换空间、磁盘)但不在物理内存中

安卓:内存不足

scrollview - SwiftUI ScrollView 无法禁用垂直弹跳?

android - Android中的ScrollView问题

android - 无尽的水平 ScrollView 表现得像轮子

android - 数据库错误 : Permission denied firebase

android - 如何禁用 Android View 中的任何事件?