I am encountering a bizarre memory bug with a Windows Forms application developed in Visual Studio 2010. I am using C# and .NET 4.0 but I don't think those are a factor here. To reproduce, start a new Windows Forms Application and add a button. Then paste in the following code:
public Form1()
{
InitializeComponent();
AllowDrop = true;
DragEnter += new DragEventHandler(Form1_DragEnter);
button1.Click += new EventHandler(button1_Click);
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Link;
}
private void button1_Click(object sender, EventArgs e)
{
for(int i = 1; i <= 100000; i++)
{
DataGridView dgv = new DataGridView(); // or any other large object
if(i % 100 == 0)
Console.WriteLine("{0}: {1} MB memory in use", i, Environment.WorkingSet / 1024 / 1024);
}
}
Run the program, hit the button, and watch the memory usage in the output window. It spikes up and then falls back down as garbage collection occurs in multiple cycles. Then run it again, drag any random file or folder onto the form until you get the link icon, and then hit the button again. This time the memory usage should increase monotonically, like garbage collection is not getting called or there are still valid references to all the objects generated.
How could setting the cursor to the link drag-drop effect affect memory usage like this???