可能重复:
.NET 垃圾收集在这里无法正常工作?
我已经测试了以下代码。单击按钮时,将创建对象并将它们添加到列表中。单击另一个按钮时,列表将被清除
GC 正在销毁对象,但内存使用量并没有减少。为什么?有什么不对?
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; }
}