Export (0) Print
Expand All

GC.GetGeneration Method (WeakReference)

Returns the current generation number of the target of a specified weak reference.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function GetGeneration ( _
	wo As WeakReference _
) As Integer
'Usage
Dim wo As WeakReference
Dim returnValue As Integer

returnValue = GC.GetGeneration(wo)
public static int GetGeneration (
	WeakReference wo
)
public static function GetGeneration (
	wo : WeakReference
) : int

Parameters

wo

A WeakReference that refers to the target object whose generation number is to be determined.

Return Value

The current generation number of the target of wo.

Exception typeCondition

ArgumentException

Garbage collection has already been performed on wo.

Imports System

Namespace GCGetGenerationWeakExample

   Class MyGCCollectClass
      Private maxGarbage As Long = 1000

      Public Shared Sub Main()
         ' Create a strong reference to an object.
         Dim myGCCol As New MyGCCollectClass

         ' Put some objects in memory.
         myGCCol.MakeSomeGarbage()

         ' Get the generation of managed memory where myGCCol is stored.
         Console.WriteLine("The object is in generation: {0}", _
                            GC.GetGeneration(myGCCol))

         ' Perform a full garbage collection.
         ' Because there is a strong reference to myGCCol, it will
         ' not be garbage collected.
         GC.Collect()

         ' Get the generation of managed memory where myGCCol is stored.
         Console.WriteLine("The object is in generation: {0}", _
                            GC.GetGeneration(myGCCol))

         ' Create a WeakReference to myGCCol.
         Dim wkref As New WeakReference(myGCCol)
         ' Remove the strong reference to myGCCol.
         myGCCol = Nothing

         ' Get the generation of managed memory where wkref is stored.
         Console.WriteLine("The WeakReference to the object is in generation: {0}", _
                           GC.GetGeneration(wkref))

         ' Perform another full garbage collection.
         ' A WeakReference will not survive a garbage collection.
         GC.Collect()

         ' Try to get the generation of managed memory where wkref is stored.
         ' Because it has been collected, an exception will be thrown.
         Try
            Console.WriteLine("The WeakReference to the object is in generation: {0}", _
                               GC.GetGeneration(wkref))
            Console.Read()
         Catch e As Exception
            Console.WriteLine("The WeakReference to the object " & _
                              "has been garbage collected: '{0}'", e)
            Console.Read()
         End Try
      End Sub


      Sub MakeSomeGarbage()
         Dim vt As Version

         Dim i As Integer
         For i = 0 To maxGarbage - 1
            ' Create objects and release them to fill up memory
            ' with unused objects.
            vt = New Version
         Next i
      End Sub
   End Class
End Namespace

package GCGetGenerationWeakExample ; 

import System.* ;

public class MyGCCollectClass
{
    private static final long maxGarbage = 1000;

    public static void main(String[] args)
    {
        // Create a strong reference to an object.
        MyGCCollectClass myGCCol = new MyGCCollectClass();

        // Put some objects in memory.
        myGCCol.MakeSomeGarbage();

        // Get the generation of managed memory where myGCCol is stored.
        Console.WriteLine("The object is in generation: {0}", 
            System.Convert.ToString(GC.GetGeneration(myGCCol)));

        // Perform a full garbage collection.
        // Because there is a strong reference to myGCCol, it will
        // not be garbage collected.
        GC.Collect();

        // Get the generation of managed memory where myGCCol is stored.
        Console.WriteLine("The object is in generation: {0}", 
            System.Convert.ToString(GC.GetGeneration(myGCCol)));

        // Create a WeakReference to myGCCol.
        WeakReference wkRef = new WeakReference(myGCCol);

        // Remove the strong reference to myGCCol.
        myGCCol = null;

        // Get the generation of managed memory where wkRef is stored.
        Console.WriteLine("The WeakReference to the object is in " 
            + "generation: {0}",
            System.Convert.ToString(GC.GetGeneration(wkRef)));

        // Perform another full garbage collection.
        // A WeakReference will not survive a garbage collection.
        GC.Collect();

        // Try to get the generation of managed memory where wkRef is stored.
        // Because it has been collected, an exception will be thrown.
        try {
            Console.WriteLine("The WeakReference to the object is in "
                +"generation: {0}",
                System.Convert.ToString(GC.GetGeneration(wkRef)));
            Console.Read();
        }
        catch (System.Exception e) {
            Console.WriteLine(
                "The WeakReference to the object has been garbage collected:" 
                + "'{0}'", e);
            Console.Read();
        }
    } //main

    void MakeSomeGarbage()
    {
        Version vt;

        for (int i = 0; i < maxGarbage; i++) {
            // Create objects and release them to fill up memory
            // with unused objects.
            vt = new Version();
        }
    } //MakeSomeGarbage
} //MyGCCollectClass

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft