Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:19:57+00:00 2026-06-14T22:19:57+00:00

Things were going well until I switched off the screen lock on my device,

  • 0

Things were going well until I switched off the screen lock on my device, then things started going wrong intermittently.

I’ve managed to track the issue down and have some workarounds in mind BUT I would like to know if there is ‘best practice’ for avoiding or removing the issue.

The issue:
I have an application which changes images based on the application status.
The images are not huge but quite large (231k~) and are stored as resources.
After several screen rotations (I counted 27 with a project using a single ImageView), loading the images fails with Exception of type ‘Java.Lang.OutOfMemoryError’

Stripped down to the barest project, the following demonstrates the problem:

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        //get a reference to the ImageView
        var imageView = FindViewById<ImageView>(Resource.Id.imageView1);
        imageView.SetImageBitmap( Android.Graphics.BitmapFactory.DecodeResource( this.Resources, Resource.Drawable.Ready) );
    }

The above code is the only method I used to reproduce the issue.

Whilst attempting to resolve, I extended the example so that imageView was released in OnDestry:

    protected override void OnDestroy ()
    {
        base.OnDestroy ();
        imageView.SetImageBitmap( null );
        imageView.DestroyDrawingCache();
        imageView.Dispose();
    }

This made no difference unless I added GC.Collect() which I don’t want to do.

The best workaround I’ve currently thought of so far would be to modify the code as follows:

    static Bitmap _ready = null;

    private Bitmap GetReadyImage {
        get {
            if (_ready == null) {
                _ready = Android.Graphics.BitmapFactory.DecodeResource (this.Resources, Resource.Drawable.Ready);
            }
            return _ready;
        }
    }

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        //get a reference to the ImageView
        imageView = FindViewById<ImageView>(Resource.Id.imageView1);
        imageView.SetImageBitmap( GetReadyImage );
    }

This relies upon a static reference to each Bitmap and a property accessor for each.

I could even write a method which stores the images in a static List to save writing property accessors for each different property/variable.

I could perhaps add the flags ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize |ConfigChanges.KeyboardHidden) but this would break the normal Activity lifecycle which I’ve read isn’t best practice?

I find it strange that having scoured the web, I’ve not yest encountered similar issues or examples. I’m left wondering how most others deal with this?

Any thoughts or comments are much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:19:58+00:00Added an answer on June 14, 2026 at 10:19 pm

    I can only approach this problem from a truly native perspective, as I have not worked directly with the Mono framework.

    The described symptoms are 100% indicative of a memory leak in the Activity, but the code shows no real evidence. If you can truly produce the issue with a project containing only one Activity and those four lines of code, it sounds to me like it is perhaps a framework bug that ought to be filed with Xamarin. Have you attempted to create the same simple project in pure Java to see how the results fare on the same device/emulator you are using? It would also be interesting to know if the issue is localized to a specific version of Android. I have never seen this particular behavior before in a native application project.

    The awkward part is your statement that forcing a garbage collection makes the problem go away. You’re right, you shouldn’t have to do that, but a true memory leak (i.e. an unreleased reference) would still persist even if you hit the GC several times. The Android paradigm of destroying and re-creating the Activity on each rotation is such that even if the old Activity lived for awhile, when memory was tight it (and all its references) would quickly be collected to make room for a new instance. If this is not happening, and each Activity is living on past even the system triggered GC passes, perhaps there is a stuck reference in the native code generated by Mono.

    Interestingly enough, technically your workaround actually does introduce a true leak, by attaching the Bitmap to a static field that is never cleared. However, I agree that in comparison it seems like a more efficient move. A simpler workaround might also be to code your Activity to manually handle configuration changes (I don’t know if Mono is different, but this is accomplished by adding android:configChanges="orientation" to the manifest file). This will keep your Activity from being recreated on each rotation, but it may also require you to reload your view hierarchy if you have different layouts for landscape and portrait. However, even if you have to do this the Acitivity instance will be the same you can safely save the Bitmap without resorting to a static field.

    However, if you cannot reproduce the problem with the same project in a native Java project, I would report a Mono bug.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started back up with Rails and things are going well up until
I started playing with Blueprint CSS tonight and things are going pretty well. Up
For a project, I had to dive into OpenCL: things are going fairly well
I am currently working on a website which was all going well until the
I'm using boto to access a dynamodb table. Everything was going well until I
While I'm going through Python code and seeing functions called, I notice things like
I'm sure most people ask why things don't work. I am goIng to mix
I was practicing how to make a singleton. Everything was going just fine until
I'm converting some C code to C#, and all is going well, however i'd
This is going to probably give me a host of downvotes, but ah well.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.