Tabbed Page causes out of memory exception

ChrisJustmanChrisJustman USMember
edited May 2016 in Xamarin.Forms

I am trying to implement a master detail page with tabs on top, which repopulate a listview. It works, but, afteryou click on 5 or 6 tabs you get an out of memory exception.

My Main Page:

public partial class RootPage : MasterDetailPage
{

    MenuPage menuPage;

    public RootPage()
    {
        menuPage = new MenuPage();

        menuPage.Menu.ItemSelected += (sender, e) => NavigateTo(e.SelectedItem as MenuItem);


        Master = menuPage;
        Master.BackgroundColor = Color.White;

        Detail = new TabsPage(menuPage.Menu.SelectedItem as MenuItem);
        Detail.BackgroundColor = Color.White;
    }

    void NavigateTo(MenuItem menu)
    {

        Detail = new TabsPage(menu);

        IsPresented = false;
    }
}

My Tabbed Page:

public class TabsPage : TabbedPage
{


    public TabsPage(MenuItem item)
    {


        //pass category id
        var dates = new Database().GetCategoryDates(item.ID);


            this.Children.Add(new EventPage(null, item.ID) { Title = "All" });

        foreach (var date in dates)
        {
            this.Children.Add(new EventPage(date,item.ID) {  Title =  date.Date.ToString("M/d") });
        }


    }

}

My Event Page:

public class EventPage : ContentPage
{


    public EventPage(DateTime? date, int catID)
    {


        List<Group> events = new Database().GetEvents(date, catID);

        var cell = new DataTemplate(typeof(EventCell));

        var template = new DataTemplate(typeof(CustomHeaderCell));

        this.Content = new ListView()
        {
            IsGroupingEnabled = true,
            GroupDisplayBinding = new Binding("Name"),
            GroupShortNameBinding = new Binding("Name"),
            GroupHeaderTemplate = template,

            HasUnevenRows = true,
            ItemsSource = events,
            VerticalOptions = LayoutOptions.FillAndExpand,
            BackgroundColor = Color.White,
            ItemTemplate = cell,
            SeparatorColor = Color.FromHex("#ddd"),
            Footer = new Image() { Source = "brewtownlogo.png", HeightRequest = 50.0 }
        };


    }

}

Answers

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @ChrisJustman - I can't see anything obvious but you certainly have a memory leak. I am not sure whether it would be in the framework or in your code somewhere, maybe in the Database class you keep creating.

    1. As such I would recommend putting the database class to something static if possible.
    2. Dispose of the Content property on the page and remove all the children manually when you create a new tabbed page.

    Otherwise you could rearchitect your design, so that instead of creating a new TabbedPage each time, your Detail page as a TabbedPage and its values are updated when you click on something, maybe using something like Messenger

  • ChrisJustmanChrisJustman USMember

    Thank you for the response. I went the route of redesigning the app as what I had I felt was very inefficient. It now works as intended.

Sign In or Register to comment.