4

I am using PerfView in order to discover memory leaks.

After comparing between two snapshot I noticed that under the tab RefTree -> static vars in PerView. MyPageDependencyProperty of mine takes 78.9% of the Inc%.

The MyPageDependencyProperty should not be there because I closed the the xaml window it is belong to.

I don't use AddValueChanged which can cause memory leak.

The DependencyProperty reveals ObservableCollection<object>.

Does anyone know can I solve this issue?

thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Guy
  • 336
  • 4
  • 18

2 Answers2

5

Instead of defining your property like this which creates a static ObservableCollection.

public static readonly DependencyProperty SomePropertyProperty  = DependencyProperty.Register(typeof(..), typeof(..),....,.... , new ObservableCollection<...>());

You should do this:

public static readonly DependencyProperty SomePropertyProperty = DependencyProperty.Register(typeof(..), typeof(..),...., null);

public MyControl()
{
   this.SomeProperty = new ObservableCollection<...>();
}

And your issue will disappear magically. :)

dev hedgehog
  • 8,698
  • 3
  • 28
  • 55
  • I have a question. Static members are associated with their types. so when the application runs, and an object is instantiated of a class, a type instance (i.e., Foo+<>c) is created as well in the memory along with its static members, right? so when a normal instance of the class is not in use, why GC is not able to remove it from the memory? I feel there is something missing I cannot grasp the whole idea. – Abdulkarim Kanaan Jul 20 '18 at 04:31
-3

This may be because of CLR has the links to the object somewhere and it doesn't Dispose. Try to force Garbage collector to run and track what will happen.

Community
  • 1
  • 1