Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a simple ScrollView to Gallery causes a memory leak

I've run into what I can only categorize as a memory leak for ScrollView elements when using the Gallery component.

A short background. I've got an existing app that is a photo slideshow app. It uses the Gallery component, but each element in the adapter is displayed in full-screen. (full source is available at this link)

The adapter View element consist of an ImageView, and two TextViews for title and description. As the photos are of a quite high-resolution, the app uses quite a lot of memory but the Gallery has in general manage to recycle them well.

However, when I am now implementing a ScrollView for the description TextView, I almost immediately run into memory problems. This the only change I made

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

I did a heap dump and could clearly see that it was the Scrollview which was the root of the memory problems.

Here are two screenshots from the heap dump analysis. Note that the ScrollView retains a reference to mParent which includes the large photo I use Heap analysis - leak candidateHeap analysis - drilldown to a single ScrollView

PS same problem occurs if I use the TextView's scrolling (android:scrollbars = "vertical" and .setMovementMethod(new ScrollingMovementMethod());

PSS Tried switching off persistent drawing cache, but no different dreaandroid:persistentDrawingCache="none"

like image 494
dparnas Avatar asked Jan 17 '12 19:01

dparnas


1 Answers

Have you tried removing the scroll view whenever it's container view scrolls off the screen? I'm not sure if that works for you but its worth a shot? Alternatively, try calling setScrollContainer(false) on the scroll view when it leaves the screen. That seems to remove the view from the mScrollContainers set.

Also, this question, answered by Dianne Hackborn (android engineer), explicitly states not to use scrollable views inside of a Gallery. Maybe this issue is why?

like image 56
Justin Breitfeller Avatar answered Sep 23 '22 07:09

Justin Breitfeller