GC.GetGeneration Metodo
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Restituisce il numero corrente di generazione di un oggetto.
GetGeneration(Object) |
Restituisce il numero corrente di generazione dell'oggetto specificato. |
GetGeneration(WeakReference) |
Restituisce il numero corrente di generazione della destinazione di uno specifico riferimento debole. |
- Origine:
- GC.cs
- Origine:
- GC.cs
- Origine:
- GC.cs
Restituisce il numero corrente di generazione dell'oggetto specificato.
public:
static int GetGeneration(System::Object ^ obj);
public static int GetGeneration (object obj);
static member GetGeneration : obj -> int
Public Shared Function GetGeneration (obj As Object) As Integer
Parametri
- obj
- Object
Oggetto per cui vengono recuperate le informazioni sulla generazione.
Restituisce
Numero di generazione corrente di obj
o Int32.MaxValue.
Esempio
Nell'esempio seguente viene illustrato come utilizzare il GetGeneration metodo per determinare l'età di un oggetto . Nell'esempio vengono quindi eseguite operazioni di Garbage Collection per pulire la memoria e confrontare i totali di memoria pre e post-raccolta nella console.
using namespace System;
const long maxGarbage = 1000;
ref class MyGCCollectClass
{
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()
{
MyGCCollectClass^ myGCCol = gcnew MyGCCollectClass;
// Determine the maximum number of generations the system
// garbage collector currently supports.
Console::WriteLine( "The highest generation is {0}", GC::MaxGeneration );
myGCCol->MakeSomeGarbage();
// Determine which generation myGCCol object is stored in.
Console::WriteLine( "Generation: {0}", GC::GetGeneration( myGCCol ) );
// Determine the best available approximation of the number
// of bytes currently allocated in managed memory.
Console::WriteLine( "Total Memory: {0}", GC::GetTotalMemory( false ) );
// Perform a collection of generation 0 only.
GC::Collect( 0 );
// Determine which generation myGCCol object is stored in.
Console::WriteLine( "Generation: {0}", GC::GetGeneration( myGCCol ) );
Console::WriteLine( "Total Memory: {0}", GC::GetTotalMemory( false ) );
// Perform a collection of all generations up to and including 2.
GC::Collect( 2 );
// Determine which generation myGCCol object is stored in.
Console::WriteLine( "Generation: {0}", GC::GetGeneration( myGCCol ) );
Console::WriteLine( "Total Memory: {0}", GC::GetTotalMemory( false ) );
}
using System;
namespace GCCollectIntExample
{
class MyGCCollectClass
{
private const long maxGarbage = 1000;
static void Main()
{
MyGCCollectClass myGCCol = new MyGCCollectClass();
// Determine the maximum number of generations the system
// garbage collector currently supports.
Console.WriteLine("The highest generation is {0}", GC.MaxGeneration);
myGCCol.MakeSomeGarbage();
// Determine which generation myGCCol object is stored in.
Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
// Determine the best available approximation of the number
// of bytes currently allocated in managed memory.
Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
// Perform a collection of generation 0 only.
GC.Collect(0);
// Determine which generation myGCCol object is stored in.
Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
// Perform a collection of all generations up to and including 2.
GC.Collect(2);
// Determine which generation myGCCol object is stored in.
Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
Console.Read();
}
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();
}
}
}
}
open System
let maxGarbage = 1000
type MyGCCollectClass() =
member _.MakeSomeGarbage() =
for _ = 1 to maxGarbage do
// Create objects and release them to fill up memory with unused objects.
Version() |> ignore
[<EntryPoint>]
let main _ =
let myGCCol = MyGCCollectClass()
// Determine the maximum number of generations the system
// garbage collector currently supports.
printfn $"The highest generation is {GC.MaxGeneration}"
myGCCol.MakeSomeGarbage()
// Determine which generation myGCCol object is stored in.
printfn $"Generation: {GC.GetGeneration myGCCol}"
// Determine the best available approximation of the number
// of bytes currently allocated in managed memory.
printfn $"Total Memory: {GC.GetTotalMemory false}"
// Perform a collection of generation 0 only.
GC.Collect 0
// Determine which generation myGCCol object is stored in.
printfn $"Generation: {GC.GetGeneration myGCCol}"
printfn $"Total Memory: {GC.GetTotalMemory false}"
// Perform a collection of all generations up to and including 2.
GC.Collect 2
// Determine which generation myGCCol object is stored in.
printfn $"Generation: {GC.GetGeneration myGCCol}"
printfn $"Total Memory: {GC.GetTotalMemory false}"
0
Namespace GCCollectInt_Example
Class MyGCCollectClass
Private maxGarbage As Long = 10000
Public Shared Sub Main()
Dim myGCCol As New MyGCCollectClass
'Determine the maximum number of generations the system
'garbage collector currently supports.
Console.WriteLine("The highest generation is {0}", GC.MaxGeneration)
myGCCol.MakeSomeGarbage()
'Determine which generation myGCCol object is stored in.
Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol))
'Determine the best available approximation of the number
'of bytes currently allocated in managed memory.
Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(False))
'Perform a collection of generation 0 only.
GC.Collect(0)
'Determine which generation myGCCol object is stored in.
Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol))
Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(False))
'Perform a collection of all generations up to and including 2.
GC.Collect(2)
'Determine which generation myGCCol object is stored in.
Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol))
Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(False))
Console.Read()
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
Commenti
Usare questo metodo per determinare l'età di un oggetto e quindi usare tali informazioni con il Collect metodo per forzare il Garbage Collector a raccogliere oggetti nella stessa generazione. Ad esempio, usare questo metodo quando si dispone di un set di oggetti creati come gruppo e che diventano inaccessibili contemporaneamente.
A partire da .NET 8, questo metodo potrebbe restituire MaxValue per gli oggetti allocati in heap non GC. Per altre informazioni, vedere GC. GetGeneration potrebbe restituire Int32.MaxValue.
Vedi anche
Si applica a
.NET 9 e altre versioni
Prodotto | Versioni |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Origine:
- GC.CoreCLR.cs
- Origine:
- GC.CoreCLR.cs
- Origine:
- GC.CoreCLR.cs
Restituisce il numero corrente di generazione della destinazione di uno specifico riferimento debole.
public:
static int GetGeneration(WeakReference ^ wo);
public static int GetGeneration (WeakReference wo);
static member GetGeneration : WeakReference -> int
Public Shared Function GetGeneration (wo As WeakReference) As Integer
Parametri
Oggetto WeakReference che fa riferimento all'oggetto di destinazione di cui determinare il numero di generazione.
Restituisce
Numero di generazione corrente della destinazione di wo
o MaxValue.
Eccezioni
La procedura di Garbage Collection è già stata effettuata su wo
.
La destinazione del riferimento debole è già stata sottoposta a Garbage Collection.
Esempio
Nell'esempio seguente viene illustrato l'utilizzo del GetGeneration metodo per determinare l'età di un oggetto riferimento debole.
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 );
}
}
using System;
namespace GCGetGenerationWeakExample
{
public class MyGCCollectClass
{
private const long maxGarbage = 1000;
static void Main()
{
// 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}", 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 = 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}", 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);
Console.Read();
}
}
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();
}
}
}
}
open System
let maxGarbage = 1000
[<AllowNullLiteral>]
type MyGCCollectClass() =
member _.MakeSomeGarbage() =
for i = 1 to maxGarbage do
// Create objects and release them to fill up memory
// with unused objects.
Version() |> ignore
// Create a strong reference to an object.
let mutable myGCCol = MyGCCollectClass()
// Put some objects in memory.
myGCCol.MakeSomeGarbage()
// Get the generation of managed memory where myGCCol is stored.
printfn $"The object is in generation: {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.
printfn $"The object is in generation: {GC.GetGeneration myGCCol}"
// Create a WeakReference to myGCCol.
let wkref = WeakReference myGCCol
// Remove the strong reference to myGCCol.
myGCCol <- null
// Get the generation of managed memory where wkref is stored.
printfn $"The WeakReference to the object is in generation: {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
printfn $"The WeakReference to the object is in generation: {GC.GetGeneration wkref}"
stdin.Read() |> ignore
with e ->
printfn $"The WeakReference to the object has been garbage collected: '{e}'"
stdin.Read() |> ignore
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
Commenti
A partire da .NET 8, questo metodo potrebbe restituire MaxValue per gli oggetti allocati in heap non GC. Per altre informazioni, vedere GC. GetGeneration potrebbe restituire Int32.MaxValue.
Vedi anche
Si applica a
.NET 9 e altre versioni
Prodotto | Versioni |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: