none
unmanaged memory leak in silverlight's navigation frame / page?

    General discussion

  • Hello! I have some trouble with a customers LOB application in Silverlight 4.
    First of all, my customers enterprise installation uses the following Silverlight 4 Runtime on all its clients: 
    Microsoft Silverlight 
    Version: 4.0.60531.0 
    (c) 2011 Microsoft Corporation. All rights reserved.
    This should be the latest GDR5 version from 2011-06-14.
    The application is huge, has lots of controls, tons of styles and pages on different assemblies as well.
    I am experiencing a huge memory usage on navigating through the application for a while. 
    The memory usage increased up to 1,5GB and may get higher.
    Thats why i started to memory profile the application.
    I found a few (business) objects not getting finalized or garbage-collected.
    After tailoring around a few days, having a look at dozens of interesting memory leak articles in this forums as well,
    i managed to minimize the memory usage a lot on the managed part, but not on the unmanaged side.
    Thats where the trouble starts.
    Just for the notes:
    - There is no "big image as thumbnail"-problem, which is often noted to check in this forums in other threads.
    The biggest image is a logo which has 40kb in the app.
    I also started to arrange the resources, since there were problems with in-line data templates.
    Now there is the main app.xaml file which is the only reference point to the style resource dictionaries.
    The resource dictionaries are located in an extra assembly.
    I still think it has something to do with the resources and silverlights navigation frame.
    Unmanaged memory is allocated on navigating to an other page. The more controls with styles used/ 
    or the bigger the resource dictionary is (where the style used is located in) the higher is the memory which is allocated.
    This memory seems to stay allocated and does not get free again.
    Thats why i started to create a mini app that shows the problem.
    Its just a mainpage with a navigation frame and to pages which can be switched by a button. Of course, pressing the button often for a long time make me mad, i added a switching-loop which now can be stopped by pressing the button.
    But not, without this loop, and just by pressing the button again and again, its the same result.
    I have also minimized the size of the styles.xaml file, which includes the resource dictionary.
    I just added 2 styles for a TextBlock- and TextBox-Control. Page 2 includes a bunch of controls with applied styles, page 1 is nearly empty.
    By switching the pages you can watch increasing the memory usage getting higher in time.
    Please note: Allocating memory in managed code like "new double[25000000]"; gets free very well on a page change. So its more the unmanaged part, thats makes the problem.
    I know that there is a caching-option for Pages, but it is no option for me,
     since the real application requires a new page load on almost all pages i can navigate to. - NO static data.
    And i assume the caching option is made for websites with pictures etc. , showing content which is not updated very often like it is done in a LOB.
    Please have a look at my sample code and the results i measured with ANTS Memory Profiler:
    Thanks so far. If you know any method to clear the unmanaged memory, please feel free to post it.
    If you have any other idea, plz feel free to post it as well.

    Hello! I have some trouble with a customers LOB application in Silverlight 4.

    First of all, my customers enterprise installation uses the following Silverlight 4 Runtime on all its clients: 


    Microsoft Silverlight 

    Version: 4.0.60531.0 

    (c) 2011 Microsoft Corporation. All rights reserved.


    This should be the latest GDR5 version from 2011-06-14.

    The application is huge, has lots of controls, tons of styles and pages on different assemblies as well.

    I am experiencing a huge memory usage on navigating through the application for a while. 

    The memory usage increased up to 1,5GB and may get higher.

    Thats why i started to memory profile the application.

    I found a few (business) objects not getting finalized or garbage-collected.

    After tailoring around a few days, having a look at dozens of interesting memory leak articles in this forums as well,

    i managed to minimize the memory usage a lot on the managed part, but not on the unmanaged side.

    Thats where the trouble starts.

    Just for the notes:

    - There is no "big image as thumbnail"-problem, which is often noted to check in this forums in other threads.

    The biggest image is a logo which has 40kb in the app.

    I also started to arrange the resources, since there were problems with in-line data templates.

    Now there is the main app.xaml file which is the only reference point to the style resource dictionaries.

    The resource dictionaries are located in an extra assembly.

    I still think it has something to do with the resources and silverlights navigation frame.

    Unmanaged memory is allocated on navigating to an other page. The more controls with styles used/ 

    or the bigger the resource dictionary is (where the style used is located in) the higher is the memory which is allocated.

    This memory seems to stay allocated and does not get free again.

    Thats why i started to create a mini app that shows the problem.

    Its just a mainpage with a navigation frame and to pages which can be switched by a button. Of course, pressing the button often for a long time make me mad, i added a switching-loop which now can be stopped by pressing the button.

    But not, without this loop, and just by pressing the button again and again, its the same result.

    I have also minimized the size of the styles.xaml file, which includes the resource dictionary.

    I just added 2 styles for a TextBlock- and TextBox-Control. Page 2 includes a bunch of controls with applied styles, page 1 is nearly empty.

    By switching the pages you can watch increasing the memory usage getting higher in time.

    Please note: Allocating memory in managed code like "new double[25000000]"; gets free very well on a page change. So its more the unmanaged part, thats makes the problem.

    I know that there is a caching-option for Pages, but it is no option for me,

     since the real application requires a new page load on almost all pages i can navigate to. - NO static data.

    And i assume the caching option is made for websites with pictures etc. , showing content which is not updated very often like it is done in a LOB.



    Please have a look at my sample code and the results i measured with ANTS Memory Profiler:


    Memory usage before switching the pages:

    Memory usage before switching the pages 470times


    Memory usage after switchting the pages 470 times:

    memory usage after switchting pages 470 times


    Please note that memory which is leaked is not that large that it is in the real LOB application. Thats, i assume, because of the small style.xaml size and less different styles used. But penetrating it often enough results in an memory increase as well.

    Here you can see the memory increase over time:

    memory profile over time

    And as you can see, waiting 30seconds or longer does not minize it a bit.


    Here is the sample project:

    http://ul.to/qjkxwkby

    Please have a look at it.


    Thanks so far. If you know any method to clear the unmanaged memory, please feel free to post it.

    If you have any other idea, please feel free to post it as well.


    Monday, July 18, 2011 7:50 AM

All replies

  • Please note:

    After further testings, i have removed the Style bindings from the xaml code and the problem still exists. The unmanaged memory increases on page changes.

    I also found that minimizing the browser results in no increase of the memory, even if the pages are still switched in background.

    If i remember correct, the Loaded event is not fired on minimized window. Maybe thats the point where we have to look at.

    Maybe it has something to do with graphic device references?

    Never the less there is an internal class called ManagedPeerTable which has a Dictionary with lots of IntPtr entries in it.

    Thx for your help.

    If you have any questions, plz feel free to reply.


    Tuesday, July 19, 2011 6:05 AM
  • There seems to be other people having the same problem:

    http://forums.silverlight.net/forums/p/200637/548535.aspx

    I tried the suggested caching option on the two pages:

    this.NavigationCacheMode = System.Windows.Navigation.NavigationCacheMode.Enabled;
    The result is the same, memory keeps increasing.
    Tuesday, July 19, 2011 8:45 AM
  • Hi Florian,

    Please try it on silverlight 5 and check if it is solved in silverlight 5 beta.

    Please let me know the result.

     

     

    Best Regards,

    Monday, July 25, 2011 5:17 AM
  • No way, enterprise rules here denie custom installation of SL5 beta. sorry

    Monday, July 25, 2011 8:59 AM
  • Hi,

     

    Did you get anywhere with this, I've been having issues with unmanaged mamory leaks for months. I thought I'd cracked it at one point when I removed the busyindicator control, and used a custom control, but it still exists. It only increases 5-10MB after 18hours use (Digital Signage App), but any increase is unacceptable.

    Saturday, September 17, 2011 12:36 PM
  • I found the following very helpful in understanding memory leaks:

    http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/learning-memory-management/resources/WPF-Silverlight-Pitfalls.pdf

    Monday, September 19, 2011 2:26 AM
  • Hi Florian,

    I tried to download your sample project that demos your memory leak issue,
    but the site you uploaded it to is asking me to register and pay in order to
    download it :(.  Could you upload it to someplace else where people can
    download it for free?  Like Dropbox for example.

    If I can download it (for free), I'll take a look at it (for free :) and see if I can spot anything.

    Vic

    Friday, October 21, 2011 11:32 PM
  • Hi Florian,

    For memory leak so many factors affect it.

    In this silverlight application we know that everything are on client side persisted except the data but what ever we are using globally in application are persist upto end.

    Now the things is if we have not used proper practices for development then also it cause the problem.

    This is not directly concern to you.. you might have use the best practices.

    even after can you please check below things in your application:

    1. all pages which you are refering are from master... means mainpage.

    2. if you are switching from one page to another page then please make sure you are not creating the same object twise. as you can refer your master page object.

    3. If you are using any thirdparty controls then please make sure about its memery usage.

    4. If you are using any resources or io operation then please make sure about it cleanup.

    5. surely check your all loop code. sometimes they are making ntimes.

    If still you have nay problem then you can direcly contact microsoft technical help center.

    Thursday, December 08, 2011 1:11 AM
  • Hi,I have same problem and I try all of your suggestions but I have still memroy leak when navigate from one page to aother page:(

    I switch my silverlight from 4 to 5 but still this problem occur:(

    Wednesday, May 02, 2012 12:59 AM