Quantcast

Memory Leak with Background Image

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Memory Leak with Background Image

subsembly
Hi,

it seems that 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 line

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 with every turn. Turning the device several times (about 7 times with a 1280x1024 image) causes an exception because we are already running out of memory.

To verify the problem 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.

I assume that Mono for Android somewhere keeps a persistent internal reference to every Activity that is ever created and never releases it. Thus all resources attached to an Activity are never released, too.

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, the memory of the background image and probably more is leaked.

BTW: Sure I could use a smaller image or maybe no background image at all. But this would only defer the problem and not solve it.

Cheers,

Andreas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Memory Leak with Background Image

Jonathan Pryor-2
On Jan 23, 2012, at 6:19 AM, subsembly wrote:
> it seems that bitmap images attached as background drawables are never released.

They're released, they're just not released soon enough. Hopefully this will provide some background and clarification:

        http://docs.xamarin.com/android/advanced_topics/garbage_collection#Helping_the_GC

If that doesn't help, please let me know how I can improve that.

In your specific case, the problem is that Mono for Android's GC hasn't executed, so after you rotate the screen a few times none of the previously created Activities have been collected, and all the Java-side memory won't be freed until the Mono for Android objects are collected.

The fix is to cause a GC by overriding Activity.OnDestroy():

        protected override void OnDestroy ()
        {
                base.OnDestroy ();
                GC.Collect ();
        }

When I add the above method to the Activity, I'm able to constantly rotate my phone without getting a crash, whereas before that change it would crash after 3 rotations.

 - Jon

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Memory Leak with Background Image

subsembly
Thanks, that did solve the problem. I also added another GC.Collect() in my OnRestart() override, just to be absolutely sure.

Cheers, Andreas.
Loading...