1

The subject "memory leaks" It's not an easy subject, and not always is possible to realize when it comes to hum memory leak.

Not yet been an analysis (Diagnostic Tool VS2015) do to my project and found that there are memory leaks, specifically a ObservableCollectios.

My collection have some time, but, I have add and remove items over application usage, however, the memory used by the application never descends on forever.

Commented line of code:

OnPropertyChanged("MyCollection")

and the memory did not rise much, so I suspect this is the problem.

Would anyone can give me a hand?

Declaration:

private ObservableCollection<MyStructure> myCollection= new ObservableCollection<MyStructure>();
        public ObservableCollection<MyStructure> MyCollection
        {
            get { return this.myCollection; }
            set { this.SetProperty(ref this.myCollection, value); }
        }

// do Add items and Remove, and soon after I make the following command: 

OnPropertyChanged("MyCollection") // To update data in UI. This command is available in INotifyPropertyChanged

thank you

fipcurren88
  • 701
  • 5
  • 26
  • You provide too little information. As you have said you raise propertychanged once the whole collection changes, eg. you assign new collection of items. In this case the bound properties are notified and the rebuild process of UI starts - new elements are created from templates and so on. As you mention about raising memory usage, probably old elements are not freed, what may depend on your implementation. – Romasz Dec 09 '15 at 18:04
  • Why do you use "ref" in set { this.SetProperty(ref this.myCollection, value); }? – Oxoron Dec 09 '15 at 18:11
  • Is there a CollectionView/Source in the loop ? if so have a look at http://stackoverflow.com/a/33960259/1463733 – Ahmad Dec 09 '15 at 18:42
  • @Romasz: yes, I suspect the elements not freed :(, but I don't know Why. – fipcurren88 Dec 10 '15 at 09:55
  • @Oxoron: is the same equals: set{ this.myCollection = value;} – fipcurren88 Dec 10 '15 at 09:55
  • It would be best if you have provided a minimal repro project. – Romasz Dec 10 '15 at 10:33
  • You use structures in your collection. So, there is possible scenario: 1. You have some collection of structures. 2. It has OnPropertyChanged, so each structure subscribe to event too. 3. When you change collection, you create new structures, they subscribe to event. Old structures do not unsubscribe. 4. You have a lot structures, subscribed to event. Try to clear old collection at the start of the setter executing. In any case, try https://www.jetbrains.com/dotmemory (it has trial) and look for the most popular object in your programm. – Oxoron Dec 10 '15 at 20:35

0 Answers0