Battling SL4 Memory Leaks

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
wint100
Posts: 3
Joined: Wed Apr 13, 2011 6:47 pm

Battling SL4 Memory Leaks

Post by wint100 » Wed Apr 13, 2011 6:48 pm

I've been trying to fix a memory leak in my SL4 app for quite some time now, and I'm at a loss as to what is causing it. The application is designed to run on a kiosk 24/7, with navigation page changes occuring every 20/30s with new charts loaded.

I've recorded a session with .NET memory profile (one of many) but can't nail down where the leak is occuring, so as a last resort, I'm hoping some kind soul can take a look at the profile data, and give me some advice on where the problem could lie.

The profile data is available for download here, it is a profile from .NET Profiler 4:



http://downloads.mwint.co.uk/downloads/ ... prfsession

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Re: Battling SL4 Memory Leaks

Post by Andreas Suurkuusk » Thu Apr 14, 2011 9:38 pm

When I opened the session file, the automatic memory analyser pointed me to a set of instances of the type Microsoft.Web.Media.SmoothStreaming.a5 that were held by a direct EventHandler. Unfortunately the type names in the SmoothStreaming library have been obfuscated, making it a bit harder to analyze. Anyway, the allocation call stack (see below) of the EventHandler revealed that the EventHandler was added in the MediaStreamSource.SetMediaElement method.

Code: Select all

System.Windows!System.Windows.Media.MediaStreamSource.SetMediaElement( void )
[Skipped frame(s)]
Microsoft.Web.Media.SmoothStreaming!Microsoft.Web.Media.SmoothStreaming.SmoothStreamingMediaElement.am( void )
Microsoft.Web.Media.SmoothStreaming!Microsoft.Web.Media.SmoothStreaming.SmoothStreamingMediaElement.ao( void,Uri )
Microsoft.Web.Media.SmoothStreaming!Microsoft.Web.Media.SmoothStreaming.SmoothStreamingMediaElement.by( void,DependencyObject )
[Skipped frame(s)]
EnergyViewerV2!EnergyViewerV2.MediaPlayerPage.Page_Loaded( void,object )
[Skipped frame(s)]
Examining this method shows that an event handler is added to Application.Exit. This event handler is never removed, which will cause a memory leak. This is very unfortunate, as this method is part of the Silverlight framework. As far as I can see, it is not possible to call any available method to correct this error. Normally you can resort to using reflection to add a hack fix, but since this is Silverlight you will probably not have the rights to access internal methods in Silverlight assemblies. So at the moment I don't have any solution for this.
The stack reducer has unfortunately skipped the frame of the method you call from Page_Loaded, so I don't know what you are doing that causes the SetMediaElement method to be called. Is there any possibility to avoid doing this for each page load? Maybe whatever you do can be reused, preferably over the lifetime of the application.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Guest

Re: Battling SL4 Memory Leaks

Post by Guest » Fri Apr 15, 2011 5:48 am

Hi,

Thanks for the info. I have noticed this myself before as well, and I haven't found a solution, but I'm glad to have your expert opinion on it.

I'll have to have a look for a way to avoid calling this on every load. The problem is I change the media source multiple times even when the page is loaded, as I may have 2-3 media sources that need to be assigned after the initial source has played.

I also noticed some unmanaged memory leaks, did you see this as well? Could this be the source of the problem if the library is obfuscated?

Thanks

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Re: Battling SL4 Memory Leaks

Post by Andreas Suurkuusk » Mon Apr 18, 2011 9:04 pm

I didn't see any unmanaged memory leaks in the session, but I didn't look for them at all. Since the unmanaged resources tracker was not enabled in your session, very little details about unmanaged memory and resources are available. If you suspect an unmanaged resource leak, I suggest that you enable the unmanaged resources tracker and run another session. This will give you more detailed information about the unmanaged resources.
Best regards,

Andreas Suurkuusk
SciTech Software AB

wint100
Posts: 3
Joined: Wed Apr 13, 2011 6:47 pm

Re: Battling SL4 Memory Leaks

Post by wint100 » Sun May 08, 2011 5:23 am

I've tried profiling with th unmanaged resource tracker enabled, but for some reason no info on unmanaged resources is collected. The real-time chart, shows no line for these resources, even when checked, and the instances doesn't change when the 'Include unmanaged resource allocations' checkbox is checked. Also, in the Overview tab, the totals in the unmanaged resources row are all empty.

Why is this, is it a Silverlight profiling limitation? I'm using the latest preview.

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Re: Battling SL4 Memory Leaks

Post by Andreas Suurkuusk » Tue May 10, 2011 9:54 pm

I'm sorry for the delay. There is a problem with the resource tracker when profiling a Silverlight application using the preview of v4.0. We have added a workaround for this problem but unfortunately it still doesn't work correctly. We're looking into this and hopefully we will come up with a solution soon.
Best regards,

Andreas Suurkuusk
SciTech Software AB

wint100
Posts: 3
Joined: Wed Apr 13, 2011 6:47 pm

Re: Battling SL4 Memory Leaks

Post by wint100 » Thu Jun 02, 2011 8:29 am

Hi,

Has anything been solved with this yet, I'm keen to profile my application for unmanaged leaks.

Thanks

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Re: Battling SL4 Memory Leaks

Post by Andreas Suurkuusk » Fri Jun 03, 2011 1:08 pm

We are most likely going to release .NET Memory Profiler 4.0 Beta next week. With the beta you should be able to use the unmanaged resources tracker for Silverlight applications.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests