Xamarin.Forms performance(One MainActivity 80+ ContentPages)

FlyFly Member ✭✭

Hi all,

I use xamarin.forms builded an APP, the APP only have one MainActivity class and has more than 80 ContentPages, every ContentPage atleast has one 'Back' button to go to previous page and every page has a override method >>>>protected override bool OnBackButtonPressed() {return true}

When i do testing for about 2 hours, the APP seems run slowly and slowly...

For example, when i done the process in page D, when i press the "Back' button go to C page, do we do some such as clean MEMORY on page D?(if yes, can give me some example code to reference?) or other process to Kill page D?

Can the MainActivity to be restart by some coding without exist the APP? or someone give me some other solutions, many thanks...

I don't have Native Android development experience,i'm a .neter(C#)

Posts

  • FlyFly Member ✭✭

    Any suggestion?

    thx......

  • JohnHardmanJohnHardman GBUniversity mod

    @Fly

    You almost certainly have memory leaks.

    There are various approaches to finding them, but it's something that's best done regularly throughout development, rather than trying to address them when the app is already large. If doing it late, you can expect to be busy for a while.

    There are various approaches to finding leaks. If you Google "xamarin forms finding memory leaks" or just "C# finding memory leaks", you'll find some useful threads. There are so many things that have to be done correctly to avoid leaks that I'm loathe to even start listing them. However, here are a few - unwire event handlers once done with them, unsubscribe from MessagingCenter subscriptions once done with them, nullify BindingContext in a Popped event handler of NavigationPages (actually, clear all sorts of things in Popped handler), be very careful if using lambda functions.

    To find leaks, you can use the profiler (if you have access to it), you can write your own poor-person's equivalent (which is what I have done), you can report the free memory each time you pop a page, or you can methodically review the code. Each approach has pros and cons. You may want to do follow more than one approach.

  • FlyFly Member ✭✭

    @JohnHardman , thanks your detail suggestions, about "nullify BindingContext in a Popped event handler of NavigationPages (actually, clear all sorts of things in Popped handler)", can you give me some sample code which you had done in your projects?

  • JohnHardmanJohnHardman GBUniversity mod

    @Fly said:
    @JohnHardman , thanks your detail suggestions, about "nullify BindingContext in a Popped event handler of NavigationPages (actually, clear all sorts of things in Popped handler)", can you give me some sample code which you had done in your projects?

    If you search the forum, there are many posts about this. Also, the source of third-party frameworks can give you a starter too.

    A couple of example links:

    https://forums.xamarin.com/discussion/comment/243491/#Comment_243491
    https://github.com/PrismLibrary/Prism/blob/master/Source/Xamarin/Prism.Forms/Common/PageUtilities.cs

  • FlyFly Member ✭✭

    @JohnHardman Thank you very much, you help me a lot!!!
    I will follow those posts and improve my APP

    Have a nice day~~

Sign In or Register to comment.