none
[UWP][C#] Need help in understanding memory map RRS feed

  • Question

  • The count of MainPage increases by 1 in the memory map every time when an app navigates back to it.

    Here is MainPage in the memory map:

    I thought the ListView in the page is the culprit.  Here is the ListView in the memory map:

    I am wondering if anyone could offer a tip on how I can find what are holding the references to prevent MainPage from being released.


    Hong

    Wednesday, July 3, 2019 3:35 AM

All replies

  • Hi,

    Does the cache mode help? Set the page.NavigationCacheMode to Required, make sure the MainPage is cached.

    Another thing is that may I know why you want to always keep the MainPage in memory? 

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 3, 2019 7:51 AM
    Moderator
  • Hi Roy,

    I want the opposite - get rid MainPage after navigating away from it.  Sorry for the confusion.

    Now, it stays in memory. If it is opened 10 times, there will be 10 instances in memory. 


    Hong

    Wednesday, July 3, 2019 11:12 AM
  • Hi,

    I'm sorry to say that it's hard to find out what reference has not been cleaned. The possible reason for this based on the image is that you had a handler for the listview or maybe a binding object for this. What have you done with the listview? Could you please tell me more about the listview?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 4, 2019 7:08 AM
    Moderator
  • Hi Roy,

    My intention is to get some tips on how to profile memory to track down the references. It sounds like what shown by the screenshots is essentially what I can do.

    Here is the ListView in Xaml. 

            <ListView Name="lvDeviceTiles"
                      ItemsSource="{x:Bind listDeviceItems}"
                      DataContext="{x:Bind}"
                      Margin="0"
                      HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                      SelectionChanged="lvDeviceTiles_SelectionChanged"
                      ScrollViewer.IsVerticalRailEnabled="True"
                      SelectedItem="{x:Bind diSelected, Mode=TwoWay}"
                      Loaded="lvDeviceTiles_Loaded" AllowDrop="True" CanDragItems="True" CanReorderItems="{x:Bind bProVersion}" 
                      Drop="lvDeviceTiles_Drop" DragItemsStarting="lvDeviceTiles_DragItemsStarting"
                      >

    lvDeviceTiles is not referenced in C# code at all. 


    Hong

    Friday, July 5, 2019 3:27 AM
  • Hi,

    First, have you unsubscribed all the events? Another thing that might related is that from the code snippet, I noticed that you are using a TwoWay mode binding for your listview. So do you implement the INotifyPropertyChanged interface? If so, could you please try to use Bindings.StopTracking() at page Unloaded event handler?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 8, 2019 6:21 AM
    Moderator
  • Hi Roy,

    Based on previous discussions, I made sure that every event subscribed to in C# is unsubscribed from upon the exit of every page. I tried Bindings.StopTracking(), but it did not make any difference.

    I tested by tearing apart of the Xaml file and found the culprit: 

    Holding="Grid_Holding" RightTapped="Grid_RightTapped"

    in 

    <ListView.ItemTemplate>
        <DataTemplate x:DataType="viewmodels:DeviceItem">
            <Grid 
                Name="gridItem"
                    Tag="{x:Bind}" 
                    Background="#88333333"
                    Margin="0"
                    Holding="Grid_Holding" RightTapped="Grid_RightTapped">
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>

    Sorry for not including this part in the previous post.  Based on previous discussions,  the event subscription in Xaml should not be a problem.  This is TRUE for the events of ListView as shown before:

    SelectionChanged="lvDeviceTiles_SelectionChanged"
    Loaded="lvDeviceTiles_Loaded"
    Drop="lvDeviceTiles_Drop"
    DragItemsStarting="lvDeviceTiles_DragItemsStarting"
    How should events subscribed to in DataTemplate be handled upon the closing of a page?


    Hong

    Monday, July 8, 2019 4:27 PM
  • Hi,

    I discussed it with my teammate, here are some suggestions the we could offer. First, could you please delete that line of code from DataTemplate to make a test to see if the problem will happen again? Second, what about setting the itemsource of the listview to null before you leave the page? If this does not work, could you please try to remove the listview out of the children of the rootpage?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 10, 2019 1:57 AM
    Moderator
  • Thanks a lot for tips and persistent help, Roy.  I will try them.

    I did lots of experiments earlier, and found the page is persisted in the memory as long as there is an event handler in DataTemplate (I tried different event handlers separately of different controls).  I even tried finding all the controls with event handlers, and unsubscribe them one by one upon the exit of the page. I also did listDeviceItems.Clear().  Nothing worked until I removed all the event handlers in DataTemplate. 

    I will try your tips and report back. 


    Hong

    Wednesday, July 10, 2019 2:27 AM
  • I have just tried setting ItemSource to null and clearing the contents including the ListView of the root control (Grid), but the the issue remains.  Right not, there is only one event handler - button click. 

    Hong

    Wednesday, July 10, 2019 3:29 AM
  • Hi,

    Well, it's not a good news. I'll involve another engineer to take a look at this. There might be some time delay. 

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 11, 2019 2:50 AM
    Moderator
  • Thanks a lot for the persistent help, Roy.


    Hong

    Thursday, July 11, 2019 1:15 PM
  • Hi,

    I just want let you know that I'm still waiting for the response from my team, I'll come back as soon as I got response.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 19, 2019 2:01 AM
    Moderator