GC.GetGeneration Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Returnerar det aktuella generationsnumret för ett objekt.
Överlagringar
| Name | Description |
|---|---|
| GetGeneration(Object) |
Returnerar det aktuella generationsnumret för det angivna objektet. |
| GetGeneration(WeakReference) |
Returnerar det aktuella generationsnumret för målet för en angiven svag referens. |
GetGeneration(Object)
- Källa:
- GC.CoreCLR.cs
- Källa:
- GC.CoreCLR.cs
- Källa:
- GC.cs
- Källa:
- GC.cs
- Källa:
- GC.cs
Returnerar det aktuella generationsnumret för det angivna objektet.
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
Parametrar
- obj
- Object
Objektet som genereringsinformationen hämtas för.
Returer
Det aktuella generationsnumret objför , eller Int32.MaxValue.
Exempel
I följande exempel visas hur du använder GetGeneration metoden för att fastställa ett objekts ålder. Exemplet utför sedan skräpsamlingar för att rensa minnet och jämföra för- och eftersamlingens minnessummor i konsolen.
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
Kommentarer
Använd den här metoden för att fastställa ett objekts ålder och använd sedan den Collect informationen med metoden för att tvinga skräpinsamlaren att samla in objekt i samma generation. Använd till exempel den här metoden när du har en uppsättning objekt som skapas som en grupp och som blir otillgängliga samtidigt.
Från och med .NET 8 kan den här metoden returnera MaxValue för objekt som allokerats på icke-GC-heaps. Mer information finns i GC. GetGeneration kan returnera Int32.MaxValue.
Se även
Gäller för
GetGeneration(WeakReference)
- Källa:
- GC.CoreCLR.cs
- Källa:
- GC.CoreCLR.cs
- Källa:
- GC.CoreCLR.cs
- Källa:
- GC.CoreCLR.cs
- Källa:
- GC.CoreCLR.cs
Returnerar det aktuella generationsnumret för målet för en angiven svag referens.
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
Parametrar
En WeakReference som refererar till målobjektet vars generationsnummer ska fastställas.
Returer
Det aktuella generationsnumret för målet woför , eller MaxValue.
Undantag
Skräpinsamling har redan utförts på wo.
Målet för den svaga referensen har redan skräp samlats in.
Exempel
I följande exempel visas hur metoden används GetGeneration för att fastställa åldern på ett svagt referensobjekt.
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
Kommentarer
Från och med .NET 8 kan den här metoden returnera MaxValue för objekt som allokerats på icke-GC-heaps. Mer information finns i GC. GetGeneration kan returnera Int32.MaxValue.