Bug 3024 - Massive Memory Leak with Background Image
Summary: Massive Memory Leak with Background Image
Status: ASSIGNED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler (show other bugs)
Version: 4.0
Hardware: Other Other
: Low enhancement
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2012-01-24 03:45 UTC by info
Modified: 2017-06-09 08:41 UTC (History)
5 users (show)

See Also:
Tags: XATriaged
Is this bug a regression?: ---
Last known good build:


Attachments

Description info 2012-01-24 03:45:43 UTC
Bitmap images attached as background drawables are never released.

In order to re-create the problem just create a default Mono for Android project and add the XML attribute

android:background="@drawable/wallpaper"

to the outer LinearLayout. Also add a wallpaper.png image (make it large to quickly see the problem, e.g. 1280x1024 pixels) to the resources drawable folder. Then start the application.

When turning the orientation of the device (portrait/landscape) the activity is re-created by Android with every turn. Turning the device several times (about 7 times with a 1280x1024 image) causes an exception and terminates the App because we are already running out of memory.

I even trimmed down the default application to just

namespace MonoAndroidApplication2
{
        [Activity(Label = "MonoAndroidApplication2", MainLauncher = true)]
        public class Activity1 : Activity
        {
                protected override void OnCreate(Bundle bundle)
                {
                        base.OnCreate(bundle);
                        SetContentView(Resource.Layout.Main);
                }
        }
}

and still, at least the memory of the background image is leaked with every turn of the device.

To verify that the problem is with Mono and not with Android itself, I also created a Java Android default application and made the very same changes to it, using the very same image. Needless to say, the real Android application does never exhaust its memory.
Comment 1 Jonathan Pryor 2013-05-23 15:45:42 UTC
The probable cause is that when the Activity is created, it's "exposed" to managed code (which takes out a GREF on it). The GREF prolongs the Activity lifetime, until Mono's GC is executed.

Furthermore, the Activity references the (large) bitmap.

The problem is that after a few rotates, there are very few objects that Mono knows about, so Mono doesn't know that a collection should be performed.

Workaround:

There are two workarounds:

1. Override Activity.OnDispose() and call GC.Collect(), so that the previously created Activity instances can be collected, reducing memory use.

2. Override Activity.OnDispose() and call this.Dispose(), which will free the GREF, allowing Java to collect the Java-side Activity + bitmap at the next Java-side collection.

(Or some variation on the above; either way, you're either explicitly invoking a GC "somewhere", or you're explicitly disposing of instances.)
Comment 3 Matthew Leibowitz 2015-10-03 18:24:56 UTC
I am having a similar issue with a grid view, so a GC.Collect is not really the best option here. 

The bound Java library (UniversalImageLoader) that I am using dies, even though it shouldn't because I have no references to the bitmap itself. A GC.Collect fixes it, but this really should be a last resort for lists.

Jonathan, I see the word "workarounds" which gives me hope for a fix? :)
Comment 4 Jonathan Pryor 2015-10-05 11:10:33 UTC
> Jonathan, I see the word "workarounds" which gives me hope for a fix? :)

Don't count on a fix anytime soon. (This bug is over 3 years old...)

The only theoretical fix I can think of is to use GC.AddMemoryPressure() and GC.RemoveMemoryPressure() so we can let the GC know that it needs to collect more frequently, so that e.g. when a 4MB Bitmap is created, the GC *knows* that a 4MB Bitmap has been created, because all our GC sees is ~48 *bytes* for that Bitmap.

The problem is that Mono's GC doesn't currently support GC.AddMemoryPressure(), so there's not much point in even prototyping this to see if it would actually help.

Once Mono supports the GC memory pressure APIs, then we can properly investigate this approach.

I don't know what the timeframe for that would be.
Comment 5 Miha Markic 2017-06-09 08:41:54 UTC
Jonathan, I've been always wondering about this. Can't Mono get that info from Java VM? By "that info" I mean memory pressure.

Note You need to log in before you can comment on or make changes to this bug.