Bug 39532 - AppCompatImageView memory leak...
Summary: AppCompatImageView memory leak...
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2016-03-10 23:41 UTC by Justin
Modified: 2017-06-28 04:25 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

ImageView leak sample app (1.35 MB, application/zip)
2016-03-10 23:41 UTC, Justin

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Justin 2016-03-10 23:41:28 UTC
Created attachment 15356 [details]
ImageView leak sample app

AppCompatImageView has a sizable memory leak.  The interesting thing is that this leak is not present with the normal ImageView.  I've attached a sample project that will allow you to see both situations.

Steps to reproduce are listed in MainActivity.cs in the project, but am also including them here for completeness:

1) Launch project while loading the Main layout
2) Open memory profiler to watch memory usage
3) Rotate the device multiple times and watch the memory usage
4) Note that memory does not continually increase
5) In my monitoring, after rotating the device multiple times, the Heap Size always returned to 
   around 9.9 MB and the Allocated Size stayed around 5.9 MB
6) Launch project again... this time loading the Main_AppCompat layout
7) Repeat steps 2 and 3
8) Note that memory continually increases after each rotation and never drops back down
9) In my monitoring, the Heap Size and Allocation Size each increased by about 5 MB after each rotation,
   and ultimately led to a crash
Comment 1 Jon Dick 2016-03-11 01:32:14 UTC
This seems possibly related: https://code.google.com/p/android/issues/detail?id=197759
Comment 2 Justin 2016-03-11 04:51:40 UTC
Definitely seems like the problem is related to that bug... :-(

I did try the suggested workarounds, but only one of them worked.  I tried these workarounds, but they didn't work:

1) Use android:background attribute instead of android:src
2) Call SetImageResource() in code
3) Call SetBackgroundResource() in code

However, the suggested workaround to calling SetImageDrawable() in code did indeed fix the memory leak problem. Here is what that code looks like:

var drawable = Resources.GetDrawable(Resource.Drawable.memory_leak);

While this fixes the memory leak problem, this is not really a great workaround, especially for layouts that are reused in multiple places...