locked
[UWP] Memory leaks in UWP ListView? RRS feed

  • Question

  • Hello,

    I'm troubleshooting a UWP app memory leaks. My conclusion so far is that ListView seems to be leaking native memory. Even with a trivial UWP app I seem to end up with native memory use trending upwards.

    I've created a sample project here: https://github.com/TheAbsurdist/uwp/tree/master/UwpMemoryLeaks

    The steps to reproduce the problem are:

    1. Click Show Page repeatedly many times.
    2. Memory use shoots up.
    3. Click Hide page.
    4. Click GC.
    5. Wait a bit and continue with step 1.

    Memory use does go up and down during the above sequence, but it doesn't seem to ever go back to the starting point. As you keep repeating the sequence, memory use keeps going up. Based on memory snapshots, the managed memory is getting released, but the native memory keeps going up.

    If I replace the ListView with manually managed StackPanel, memory use seems to get under control. Also, if I remove image from the ListItem, memory use will still go up, but slower.

    There are couple of other UWP memory leak related threads, but neither of them seems to have a clear conclusion (I'm clearing ItemSouce, for example, doesn't help):
    • https://social.msdn.microsoft.com/Forums/en-US/92f76519-f139-4c94-ae5e-630f0abda66e/uwpcmemory-leak-with-listview-ui-virtualization?forum=wpdevelop
    • https://social.msdn.microsoft.com/Forums/windowsapps/en-US/ecbe74c9-d6ae-4c66-b0f6-6a942451527e/uwp-platform-issue-memory-leaks-in-uwp-c-applications?forum=wpdevelop

    I've tried all kinds of tricks, which I removed from the sample app, but nothing seems to work. Currently troubleshooting the memory leaking of an actual app is near impossible since I can't get a trivial sample app memory use under control.

    Questions:

    1. Is this memory leaking a known problem? (Seems to be?)
    2. Does the sample app some obvious flaw that results in the memory leaks?
    3. If there are native side memory leaks in UWP ListViews, what would be the workaround?
    Tuesday, August 1, 2017 5:48 PM

Answers

  • Hi TheAbsurdist,

    This issue seems to be resolved in the Fall Creators update version(OS version 1709 OS build 16299), you can have a check with it.

    Best regards,

    Breeze


    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.

    • Marked as answer by TheAbsurdist Thursday, October 18, 2018 8:39 PM
    Tuesday, November 21, 2017 7:39 AM

All replies

  • Hi TheAbsurdist,

    In your project, every time you create a new ContentPage in the showPage_Click event, it will cause the memory goes up. If you don't create too much ContentPage object, it should be well.

    The garbage collector determines when to run by balancing the memory consumption of the managed heap with the amount of work a garbage collection needs to do. See Improve garbage collection performance.

    Best regards,

    Breeze


    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, August 2, 2017 9:44 AM
  • Hi Breeze Liu,

    Yes, creating a new ContentPage will cause memory go up. I should be able to create ContentPage millions of times, since the old ContentPage gets removed from the visual tree when a new one is created, and the old one should be eventually garbage collected and memory use should stabilize. What I'm seeing is that something of the old pages in native memory gets retained and not garbage collected. In addition, running GC explicitly doesn't seem to free this memory, and neither does waiting for 10-20 minutes or so, which leads me to think there must be memory leaking going on.

    If I implement the same scenario by adding items as StackPanel children, replacing ContentPage with a new one will result GC reclaiming all of the memory.

    The "Improve garbage collection performance" asks to remove references. That is my question 3 above. What is the reference I need to remove in my sample app in order to reclaim the native memory?

    Wednesday, August 2, 2017 6:14 PM
  • Here are some memory stats based on Visual Studio's Process Memory (MB) display. Debug build was used.

    Initial memory use, after ContentPage was loaded: 57 MB. After this I spend about 10 minutes repeating the sequence above and sampled the memory use between steps 4 and 5. The sequence I got was: 59, 63, 65, 70, 73, 76, 79, 80, 86, 89. After each sequence memory went up about 3MB (didn't sample after every repeat, but the pattern seems clear).

    After taking the above sequence I let the app run for 1h 30min. After this wait the memory use was at 88 MB, so it went down 1MB.

    I've created an equivalent WPF example here: https://github.com/TheAbsurdist/uwp/tree/master/WpfExample

    I did the same experiment with the WPF app. Initial memory use, after ContentControl was loaded: 66 MB. The sequence I got was: 73, 73, 74, 74, 74, 74, 74, 74, 74. Looks like the memory use stabilizes, which is what I would expect on UWP as well, given the sample app's use case.

    What would explain the difference between UWP and WPF?

    My next step with UWP would be to use a timer to do the steps in the sequence and see how high I can take the memory use in, say, 24 hours.



    Wednesday, August 2, 2017 10:21 PM
  • Hi TheAbsurdist,

    In my test with your uwp app, it is not memory leaks, it stays in a stable value. As the following image, the GC works well to collect the memory and the memory does not keep going up. I can not reproduce your issue that the memory keeps rising.

    Best regards,

    Breeze


    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.

    • Edited by Breeze Liu Thursday, August 3, 2017 8:24 AM
    Thursday, August 3, 2017 8:22 AM
  • Hi Breeze Liu,

    You need to repeat the cycle many times and you should see an overall upward trend in memory consumption. The image you pasted seems to be the first sequence from the very start.

    Thursday, August 3, 2017 5:06 PM
  • Added two new projects:

    1. https://github.com/TheAbsurdist/uwp/tree/master/UwpMemoryLeaksAuto
    2. https://github.com/TheAbsurdist/uwp/tree/master/WpfExampleAuto

    Both of these projects automate the test sequence, so you only need to click start.

    (Looks like this forum doesn't allow me to add images, so I added the images to Github)

    I let UwpMemoryLeaksAuto run for 30 minutes and got the following result:

    https://github.com/TheAbsurdist/uwp/blob/master/Images/uwp_auto_30min_session.png

    I let WpfExampleAuto run for 30 minutes and got the following result:

    https://github.com/TheAbsurdist/uwp/blob/master/Images/wpf_auto_30min_session.png

    WpfExampleAuto was stable during the entire test run. It seemed to stabilize at  81.9MB after the GC step.

    When contrasting the results, it should be clear that UWP memory use doesn't stabilize and keeps increasing as the test sequence runs.


    Thursday, August 3, 2017 8:20 PM
  • Hi TheAbsurdist,

    Thanks for reporting this issue, I can see the memory going up and achieve a higher level, I have reported it. It needs some time for investigating into it. Appreciate your patience in advance. Thanks for your understanding.

    Best regards,

    Breeze 


    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, August 4, 2017 7:30 AM
  • Hi Breeze,

    Any suggestions for a workaround? I would imagine a workaround requires explicit memory management, I just don't know what that would be. Dropping down to C++ level for the workaround is acceptable.

    Monday, August 14, 2017 4:44 PM
  • Hi TheAbsurdist,

    This issue seems to be resolved in the Fall Creators update version(OS version 1709 OS build 16299), you can have a check with it.

    Best regards,

    Breeze


    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.

    • Marked as answer by TheAbsurdist Thursday, October 18, 2018 8:39 PM
    Tuesday, November 21, 2017 7:39 AM
  • Hello,

    I am sorry to Reply to this thread a year later. But! It seems not to be resolved.
    I have created a test application which consumes massive Memory for some reasons...

    Just imagine. I have a ListView with 3 columns. 1 Image and 2 Textblocks.

    The Image is a SVG file which has a size 5kb.

    Each insert to the listview adds around 5mb of allocated Memory.
    When I remove the object from the list and null the object, nothing is going to happen.
    You can check out my Code here:


    CS
    https://hastebin.com/nagovevila.cs

    XAML
    https://hastebin.com/ebemuyekid.xml

    I have created 25 entries and deleted 10 of them. 
    What could cause this massive consumption?

    OS info:

    Windows 10 1809
    Windows 10 Insider SDK for 1809




    • Edited by Dogzilla Monday, October 22, 2018 10:46 AM
    Monday, October 22, 2018 10:44 AM
  • Hello Breeze,

    I am facing the same issue but in my case the device which we are targeting is "Janam XT-2" having Windows 10 version-1607 and OS build 10.0.14393.2551.

    There is no option for any update that I can see in the device.

    Is there any way to stop the memory leak in this device.

    Wednesday, June 19, 2019 6:12 AM