Ahmed_Faraz:
I am working on WPF project where I need to continuously update a ListView. The problem is that there is a small memory leak whenever I update the List. I have made the following sample code where I simply update the ListView whenever the timer expires.
Related Articles
public partial class MainWindow : Window
{
private ObservableCollectiondummyData = new ObservableCollection ();
private System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
public MainWindow()
{
InitializeComponent();
dummyData.Add("abc");
dummyData.Add("abcd");
dummyData.Add("xyz");
dummyData.Add("abc1");
dummyData.Add("abc2");
dummyData.Add("abc3");
dummyData.Add("abc4");
dummyData.Add("abc5");
dummyData.Add("abc6");
dummyData.Add("abc7");
dummyData.Add("abc8");
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += timer_Tick;
timer.Start();
}
public ObservableCollectionDummyDataCollection
{
get
{
if (dummyData == null)
{
dummyData = new ObservableCollection();
}
return dummyData;
}
set
{
dummyData = value;
}
}
void timer_Tick(object sender, EventArgs e)
{
lvDataBinding.ItemsSource = null;
lvDataBinding.ItemsSource = dummyData;
}
}
Now when I run this application, the memory used by it keeps on increasing.Even though it increases in very small amount but I cannot ignore it in my project.
So my question is whether or not this the correct way to update the List. If yes then how can I avoid the memory leak
Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE