Request garbage collection : Garbage Collection « Development « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Development » Garbage CollectionScreenshots 
Request garbage collection
Request garbage collection
  
Imports System

Public Class MainClass

    Shared Sub Main(ByVal args As String())

      Console.WriteLine("Students before instantiation: " & Student.Count)

      Dim student1 As Student = New Student("A""B")

      Dim student2 As Student = New Student("C""D")

      ' output of student2 after instantiation
      Console.WriteLine("Students after instantiation: " & vbCrLf & _
         "via Student.Count: " & Student.Count)

      ' display name of first and second student
      Console.WriteLine(vbCrLf & "Students 1: " & _
         student1.FirstName & " " & student1.LastName & _
         vbCrLf & "Student 2: " & student2.FirstName & " " & _
         student2.LastName)

      ' mark student1 and student2 for garbage collection
      student1 = Nothing
      student2 = Nothing

      System.GC.Collect() ' request garbage collection
      
    End Sub
End Class

' Class Student uses Shared variable.

Class Student
   Inherits Object

   Private mFirstName As String
   Private mLastName As String

   ' number of objects in memory
   Private Shared mCount As Integer

   ' Student constructor
   Public Sub New(ByVal firstNameValue As String, _
      ByVal lastNameValue As String)

      mFirstName = firstNameValue
      mLastName = lastNameValue

      mCount += ' increment shared count of students
      Console.WriteLine _
         ("Student object constructor: " & mFirstName & _
         " " & mLastName)
   End Sub ' New

   ' finalizer method decrements Shared count of students
   Protected Overrides Sub Finalize()
      mCount -= ' decrement mCount, resulting in one fewer object
      Console.WriteLine _
         ("Student object finalizer: " & mFirstName & _
         " " & mLastName & "; count = " & mCount)
   End Sub ' Finalize

   ' return first name
   Public ReadOnly Property FirstName() As String

      Get
         Return mFirstName
      End Get

   End Property ' FirstName

   ' return last name
   Public ReadOnly Property LastName() As String

      Get
         Return mLastName
      End Get

   End Property ' LastName

   ' property Count
   Public Shared ReadOnly Property Count() As Integer

      Get
         Return mCount
      End Get

   End Property ' Count

End Class ' Student
           
         
    
  
Related examples in the same category
1.Object GenerationObject Generation
2.Force a garbage collectForce a garbage collect
3.GC Suppress Finalize meGC Suppress Finalize me
4.Force Garbage CollectionForce Garbage Collection
5.Garbage collection startedGarbage collection started
6.Get GC generation
7.GC Class
8.Determine which generation object is stored in
9.Determine the best available approximation of the number of bytes currently allocated in managed memory
10.Perform a collection of generation 0 only
11.GC.GetGeneration returns the current generation number of the target of a specified weak reference.
w_w___w___.j__a__v_a__2_s___.__c__o___m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.