Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

GC::GetGeneration Method (WeakReference)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public:
static int GetGeneration(
	WeakReference^ wo
)

Parameters

wo
Type: System::WeakReference

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

Return Value

Type: System::Int32
The current generation number of the target of wo.
ExceptionCondition
ArgumentException

Garbage collection has already been performed on wo.

The following example demonstrates the use of the GetGeneration method to determine the age of a weak reference object.

using namespace System;
ref class MyGCCollectClass
{
private:
   static const long maxGarbage = 1000;

public:
   void MakeSomeGarbage()
   {
      Version^ vt;
      for ( int i = 0; i < maxGarbage; i++ )
      {

         // Create objects and release them to fill up memory 
         // with unused objects.
         vt = gcnew Version;

      }
   }

};

int main()
{

   // Create a strong reference to an Object.
   MyGCCollectClass^ myGCCol = gcnew 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.
   WeakReference^ wkref = gcnew WeakReference( myGCCol );

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

   // 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 ( Exception^ e ) 
   {
      Console::WriteLine( "The WeakReference to the object has been garbage collected: ' {0}'", e );
   }

}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.