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
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
[Activity(Label = "MonoAndroidApplication2", MainLauncher = true)]
public class Activity1 : Activity
protected override void OnCreate(Bundle bundle)
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.
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.
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.)
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? :)
> 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.
Jonathan, I've been always wondering about this. Can't Mono get that info from Java VM? By "that info" I mean memory pressure.