Assume we have a single Windows form with a button called SimpleButton1. The following code results in an uncontrollable amount of memory usage. What am I doing incorrectly?
My understanding is that at every iteration of the for loop, GC cleans up any TestClass objects, and would take care of any associated events as well since there are no handlers on any of the events
Public Class Form1
Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
For i = 1 To 1000000
Dim test1 As New TestClass
Next
End Sub
End Class
Public Class TestClass
Private Event TestEvent(ByVal sender As Object)
End Class
Note that I have tried implementing IDisposable and calling test1.Dispose() at the end of every iteration of the For loop, but I suspect I am not disposing the correct resources.
*ANSWERED: There was no problem in the code, and it was run as expected. Problem was that I was running it in debug mode, and the overhead that is created results in large memory usage. See discussion below.