This is a migrated thread and some comments may be shown as answers.

Static property and GC : Memory Leak?

2 Answers 137 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Deepak Shakya
Top achievements
Rank 1
Deepak Shakya asked on 24 Aug 2012, 08:05 AM
Hi,

I am writing a Silverlight navigational application using MVVM framework. I am using JustTrace to track memory leaks. Hence my post in this forum.

Scenario:
  • I have a static Helper class which has a static property that refers to a class. i.e. if I have a class DefinedClass, the property is 
  • public static DefinedClass MyClass
  • I am using this to store application variables that will be populated from different views using corresponding viewmodels. 
  • In the first view, I populate properties in MyClass. This is done using MyClass as one of the properties in viewmodel.
  • On UnLoading, the property is set to null so that it does not hold the reference to the static class.
  • This goes fine and I do not see any instance of the viewmodel in memory snapshot.

Now when I navigate away from the initial view to a new view, the new view's viewmodel also has a property which will be assigned the value from the static field in constructor.
  • this view/viewmodel makes some changes and then navigates to the previous view.
  • on UnLoading, again, MyClass is set to null to break any references to the static variable.

But this time, in the memory snapshot, the second viewmodel's instance is retained and increments by one every time I visit this view.
In the memory snapshot, it shows that it still holds reference to the static field.

How do I release the reference to the static field so that the second viewmodel is garbage collected?

Hope I have explained enough.

Thanking you in advance.

2 Answers, 1 is accepted

Sort by
0
Deepak Shakya
Top achievements
Rank 1
answered on 27 Aug 2012, 01:40 AM
Hi all,

Just wanted to let you know that the memory issue is solved. The culprit was event subscription. I have subscribed to PropertyChanged event for a model so that everytime any property changes, it should recalculate the final values. This is to do on-the-fly calculation rather than populating the form and hitting OK button to calculate.

I wasn't unsubscribing to the event and since it was writing the values to static property, it was still holding the reference in the memory when navigated to different view. For now, it is solved and I only have one instance of a particular viewmodel. 

Cheers!
0
Accepted
Martin
Telerik team
answered on 27 Aug 2012, 08:09 AM
Hi Deepak,

I'm happy to hear that you have resolved the memory leak.

Indeed unsubscribed events are often the reason for memory leaks. Another common scenario for memory leaks is binding to a CLR property of a class which is neither a DependancyObject, nor implements INotifyPropertyChanged (you can read this article for a description of the issue: http://support.microsoft.com/kb/938416)

All the best,
Martin
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Deepak Shakya
Top achievements
Rank 1
Answers by
Deepak Shakya
Top achievements
Rank 1
Martin
Telerik team
Share this question
or