0

i have this small piece of code where i would expect that the GC at a certain point would wipe the memory, instead i run out of memory.

Is this a correct behaviour for the GC?

Private Sub Form1_Load()
   Dim WasterWrapper as cMyClass
   For MapIndex = 1 To 50
      WasterWrapper = New cMyClass
   Next
End Sub

this is the class which allocates memory

Public Class cMyClass


Private mArry(,) As Double

Sub New()

    Dim i As Integer
    Dim j As Integer

    ReDim mArry(5000, 5000)

    For i = 0 To 5000
        For j = 0 To 5000
            mArry(i, j) = Rnd() * 1000
        Next
    Next

End Sub

Protected Overrides Sub Finalize()

    MsgBox("Finalising the wrapper")
    MyBase.Finalize()

End Sub
End Class
user1964154
  • 416
  • 1
  • 4
  • 10
  • 1
    The memory isn't freed automatically. GC has to run, which it will when it feels it's necessary. I noticed you implemented the finalize method. That will add to the time needed before it's freed because the object is going to be placed in the finalize queue before being freed. You can try `GC.Collect` for you tests to force GC to run. But be wary of using this under normal circumstances, it's not recommended. – Jason Tyler Jan 09 '13 at 17:53

1 Answers1

0

After I removed the Finalize() now the GC can free the memory as i expected.

Thanks for the hint.

user1964154
  • 416
  • 1
  • 4
  • 10