Static property and GC : Memory Leak?

Thread is closed for posting
3 posts, 1 answers
  1. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 24 Aug 2012 Link to this post


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

    • 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. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 26 Aug 2012 Link to this post

    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. 

  3. Answer
    Martin avatar
    31 posts

    Posted 27 Aug 2012 Link to this post

    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:

    All the best,
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top