Possible Duplicate:
.NET garbage collection not working properly here?
I’ve tested the following code.
when the button is clicked, objects are created and add them to the list. When another a button is clicked, the list is cleared
GC is destroying the objects, but memory usage does not decrease.
Why? Is there something wrong?
public partial class MainWindow : Window
{
public List<MyModel> MyList;
public MainWindow()
{
MyList = new List<MyModel>();
InitializeComponent();
}
private void button1_Click_1(object sender, RoutedEventArgs e)
{
MyList = new List<MyModel>();
for (int i = 0; i < 1000000; i++)
{
MyList.Add(new MyModel { ID = i, Description = "Model Id : " + i.ToString() });
}
MessageBox.Show("Complate!");
}
private void button1_Click_2(object sender, RoutedEventArgs e)
{
MyList.Clear();
MyList = null;
}
}
public class MyModel
{
~MyModel()
{
Console.WriteLine(Description);
}
public int ID { get; set; }
public string Description { get; set; }
}
Suppose you own a warehouse. You store boxes in the warehouse. Some of the boxes are garbage, so periodically you throw them out. The square footage of the warehouse does not get smaller.
If what you’re measuring is the total number of memory pages committed to the process, then you’re not measuring the number of boxes in the warehouse, you’re measuring the square footage of the warehouse.