Thanks for your response.
I am unable to refactor my code for the RadChartView at this time since there is no migration path for it. Perhaps in a later version I'll have time in my schedule to do that.
I did however come up with a solution to stop the memory leak with RadChart and MVVM. I will share this so others in the community can benefit from it.
In a nutshell, when updating RadChart dynamically from a database or any real time data source, you cannot simply clear your ObservableCollection and refill with new values since RadChart binds to them and never lets go. This was the primary source of the memory leak. Every update cycle I would Clear() the collection in my ViewModel and reload with a new set of data from the Model. In my scenario database tables are cleared each update cycle and all rows are deleted before updating with new data.
The solution is to reuse items in the ObservableCollection and let the property changed mechanism work. When clearing items in the collection and reloading with new objects, RadChart must re-bind to each new object. This must be the default behavior of the QueryableCollection in RadChart.
Here's a code snippet that illustrates it:
_dc.ocReserve is a List<EntityType> from the EntityFramework. This gets updated in a background thread along with the ViewModel which contains the ObservableCollection n1 that is bound to the RadChart. The trick is to reuse an existing item in the collection instead of clearing it and let the INotifyPropertyChanged mechanism update RadChart from the Model. (Core4Model in this case)
(_dc.ocReserve.Count > 0)
//reuse existing slots in n1
// n1 is the ObservableCollection
(n1.Count > 0 && y < n1.Count)
n1[y].PartySize = x.PartySize;
n1[y++].ReservationTime = x.ReservationTime;
// or create a new one
(_dc.ocReserve.Count > n1.Count)
Core4Model m =
m.PartySize = x.PartySize;
m.ReservationTime = x.ReservationTime;
// if we have less items in the source than the collection let's remove the old ones
(_dc.ocReserve.Count < n1.Count)
i = n1.Count - _dc.ocReserve.Count; i != 0; i--)
n1.RemoveAt((y - 1) + i);
//in the event there is nothing in the database go ahead and clear it
This simple reuse pattern has eliminated the leak. Luckily in my case database rows tend to grow and not shrink because any shrinkage will still cause a small memory leak.
Again, this is a workaround for using RadChart with dynamic updates and MVVM. BTW, the latest Telerik Visual Studio add-in I used dropped the RadChart on my page right out of the gate. Perhaps you should consider switching to the RadChartView since this is what you recommend moving forward.