Has anyone seen a crash in Android when setting the App.MainPage property too many times?

I'm running into an issue only on Android (iOS works just fine) where if I set the MainPage property too many times, the App crashes with an unexpected error and no useful information in the call stack. (Entire call stack is from external code) Anyone seen how to catch unexpected exceptions in the android underpinnings of the xamarin forms app?

Answers

  • PaulDistonPaulDiston USUniversity ✭✭✭✭

    Hi,

    Would you be able to share a sample which reproduces this crash?

    Thanks

    Paul Diston

  • Sorry, this turns out to be more due to the two listviews on each page. There appears to be a memory leak on android involving listviews with custom cells. I did some investigating and found that if I did the following in my pages:

        protected override void OnAppearing()
        {
            BindingContext = controller.Model;
            base.OnAppearing();
        }
    
        protected override void OnDisappearing()
        {
            BindingContext = null;
            Content = null;
            base.OnDisappearing();
            GC.Collect();
        }
    

    Set the android option in the manifest to use a large Heap (android:largeHeap="true") , and lastly, used the new Garbage collector, (in the environment.txt, MONO_GC_PARAMS=bridge-implementation=new)
    This combination of things, seems to fix the crashing issue. I can only assume that this is just because the setting of things to null, helps the GC to dispose of the elements, the large heap size to buy the GC time to do so, and the new GC option to help accelerate the collection itself. I sincerely hope this helps someone else...

Sign In or Register to comment.