I have a program that uses a ListView to print a large amount of data to the user interface. It can fill up to hold more than 500MB of data sometimes, but this isn’t my problem. My problem is that if I try to clear the items in the listview via listview.Items.Clear(); the memory consumption of my application only goes down a fraction, and sits there. Memory examples from a brand new windows forms application with just a listview in it:
- 7,812K – Program run with no listview items added.
- 26,816K – Populated with 50000 ListView items assigned to the value “test”
- 22,860K – Memory after clearing the previous 50000 ListView items.
Memory values are from Task Manager.
My problem is that listview.Items.Clear() isn’t freeing all of the memory taken by the listview items, as far as I can tell.
Relevant information: In my test program, all it contains is a ListView with the ‘details’ view property, and one column.
Relevant code:
public void Test()
{
for (int i = 0; i < 50000; i++)
{
listView1.Items.Add(new ListViewItem("test"));
}
}
private void listView1_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
}
Clearing the listview doesn’t dispose the items, that is the reason why the memory stays the same.
However, if the items are not referenced anywhere in your project, when garbage collector kicks in they will be disposed properly and the memory will go down again.