views:

239

answers:

2

In our SL4 application built on Caliburn.Micro, we've come across a (another) memory leak.

Stripped out, it seems that the problem is caused by ItemsControl with custom DataTemplate bound to an IEnumerable collection of objects that implement INotifyPropertyChanged interface.

When the source collection is changed (another collection is assigned to the ViewModel's property that the ItemsControl's ItemsSource is bound to), the entites in the original collection and bound DataTemplates are not garbage collected. Although the event handling of NotifyPropertyChanged seems to be done internally via WeakReference, it is like SL is keeping another reference to these objects. So every time we refresh data from the server, memory consumption increases.

Do you have any idea how to solve this problem? I really cannot understand how can this kind of bug happen in SL4!

Some experiments suggested that calling ItemsControl.Items.Clear() could help. Any tip how to simply call this every time the ItemsSource is changed? The only thing that comes to my mind would be to override ItemsSourceProperty and add a handler there.

EDIT: It turned out that the leak occurs in this situation:

  • load entities through RIA services context and store a collection of them in a property of viewmodel
  • bind a listview with custom data template to the property with collection of entites
  • refresh the entites via RIA services context

What happens is that although the entities get refreshed, which can be seen in the view, memory consumption is rising.

If there is no binding, refreshing the entities does not eat more memory (it might but the memory consumption level eventually returns back as GC does its job).

If you clear the context or simply create a new one, the memory is also eventually collected.

It seems that the problem is connected with RIA services.

I can provide a simple project that shows the problem if you want.

UPDATE: The memory leak seems to be caused by INotifyDataErrorInfo. Read here.

A: 

UPDATE:

Silverlight 4 Service Release to fix Memory Leaks: http://timheuer.com/blog/archive/2010/09/01/silverlight-service-release-september-2010-gdr1.aspx

Silverlight 4 has known memory leak issues with data templates. There is a fix on the way it's currently being tested.

Here is a thread I have been following:

The user "heuertk" is a Microsoft Silverlight Developer....he explains the issues and the status of the fix...

http://forums.silverlight.net/forums/t/171739.aspx

gmcalab
A: 

It is my understanding that this particular issue is a bug in Silverlight 4 itself. However, you say you've come across another memory leak. Did you determine if that one was related to Caliburn.Micro and if so, have you posted about it in the projects forums? If the fault is in CM, I'd like to try and fix that. Thanks.

EisenbergEffect
Hi, Rob. Well, although it is not the well-known SL4 bug solved by the latest release GDR1, it seems that it is connected with RIA services. See the updated question. Anyway, thank you very much for Caliburn-Micro - it is really great! If the leak was caused by CM I would firstly try to fix it myself and then send you the fix :-).
gius
Hi, Rob. We have eventually tracked the memory leak - it is not caused by C.Micro but by SL itself (INotifyDataErrorInfo). I've blogged about it: http://baud.cz/blog/memory-leak-in-silvelright-with-inotifydataerrorinfo
gius