VB.Net Tutorial/Development/GC

Материал из VB Эксперт
Перейти к: навигация, поиск

GC

<source lang="vbnet">public class Test

  public Shared Sub Main
       Dim col As New Collection()
       Dim obj As Class1
       For x As Integer = 1 To 100
           obj = New Class1(col)
       Next x
       Console.WriteLine("Collection contains " + col.Count.ToString() + " objects")
       col = Nothing
       obj = Nothing
       GC.Collect()
       GC.WaitForPendingFinalizers()
  End Sub

End class Public Class Class1

   Shared m_classcount As Integer
   Dim m_ClassId As Integer
   Dim m_Collection As Collection
   Public Sub New(ByVal mycontainer As Collection)
       MyBase.New()
       mycontainer.Add(Me)
       m_Collection = mycontainer
       m_ClassId = m_classcount
       m_classcount = m_classcount + 1
   End Sub
   Protected Overrides Sub Finalize()
       System.Diagnostics.Debug.WriteLine("Destructed " & m_ClassId.ToString())
   End Sub

End Class</source>

Collection contains 100 objects

Get Total Memory

<source lang="vbnet">Module Module1

   Sub Main()
       Console.WriteLine("Starting Heap Space: " & GC.GetTotalMemory(False))
       Dim BigArray(50000) As Byte
       Dim BiggerArray(250000) As Byte
       Console.WriteLine("Heap Space After Arrays: " & GC.GetTotalMemory(False))
       BigArray = Nothing
       Console.WriteLine("Final Heap Space: " & GC.GetTotalMemory(True))
   End Sub

End Module</source>

Starting Heap Space: 279556
Heap Space After Arrays: 587808
Final Heap Space: 283804