(Unreleased ?) memory from listview in modal

ReinVReinV BEMember ✭✭

Hello everyone,

Playing around with some listviews, modals and apple instruments I observed some memory that never seem to be released over the course of the application.

It is a page with a button that opens a modal with 100000 items in it.
Selecting an items closes the modal and return to the initial page.

Doing so the first time there is a +/- 2mb of data staying in memory

Subsequent times there is a +/- 850kb staying in memory

I have added some screenshots demonstrating this.

The 'peaks' are when the modal is open.

My knowledge of these things is not so good so my question would be if this is something I can resolve, should not worry about or anything else.

The code to 'reproduce' this:

App.cs

    public class App : Application
    {
        public App ()
        {
            MainPage = new NavigationPage(new MainPage());
        }
    }

MainPage.cs

    public class MainPage : ContentPage
    {
        public MainPage()
        {
            var listData = new List<string> ();

            for (int i = 1; i <= 1000000; i++) {
                listData.Add(String.Format("Data {0}", i));
            }

            var listViewButton = new Button {
                Text = "List View Modal"
            };
            listViewButton.Clicked += async (object sender, EventArgs e) => await Navigation.PushModalAsync (new ListPage (listData), false);

            Content = new StackLayout {
                VerticalOptions = LayoutOptions.Center,
                Children = {
                    listViewButton
                }
            };
        }
    }

ListPage.cs

    public class ListPage : ContentPage
    {
        ListView listView;
        public ListPage(List<string> data)
        {
            listView = new ListView {
                ItemsSource = data
            };

            Content = listView;
        }

        protected override void OnAppearing ()
        {
            base.OnAppearing ();
            listView.ItemSelected += ListView_ItemSelected;
        }

        protected override void OnDisappearing ()
        {
            base.OnDisappearing ();
            listView.ItemSelected -= ListView_ItemSelected;
        }

        async void ListView_ItemSelected (object sender, SelectedItemChangedEventArgs e)
        {
            if (e.SelectedItem == null)
                return;

            await Navigation.PopModalAsync(false);
        }
    }

Answers

  • ReinVReinV BEMember ✭✭

    PS: The same kind of results appear with a 100 items in the list

    First time: +/- 2mb
    Consecutive times: +/- 850kb

  • ReinVReinV BEMember ✭✭

    Anyone that could explain what is going on ?

    In our app we have alot of different listview getting shown and I'm worrying that this could lead to problems.

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    Try setting the Listview i
    ItemSource =null in the OnDisappearing Method. Does it change anything?

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    Also you could reuse the same Page instead of creating it every time the Button is pressed.

  • ReinVReinV BEMember ✭✭

    @ThomasBurkhart said:
    Try setting the Listview i
    ItemSource =null in the OnDisappearing Method. Does it change anything?

    I did try that and it didn't seem to change the outcome.

  • ReinVReinV BEMember ✭✭

    @ThomasBurkhart said:
    Also you could reuse the same Page instead of creating it every time the Button is pressed.

    What techniques should I be using for reusing a page ? (I've always newed up pages I needed untill now)

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    @ReinV Instead of creating the Page inside the PushModalAsync. Create it before and store it in a Member variable. Then just push it the next time again.

  • ReinVReinV BEMember ✭✭

    @ThomasBurkhart said:
    @ReinV Instead of creating the Page inside the PushModalAsync. Create it before and store it in a Member variable. Then just push it the next time again.

    Using this, will the memory be freed after PopModalAsync ?

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    No, but it won't be allocated again when you Press the Button the next time.

  • ReinVReinV BEMember ✭✭

    @ThomasBurkhart said:
    No, but it won't be allocated again when you Press the Button the next time.

    Ok, Thank you for your time !

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    @ReinV No Problem. It's always a nice gesture to like a post if it helps you.

Sign In or Register to comment.