GC.GetGeneration 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回对象的当前代数。
重载
GetGeneration(Object) |
返回指定对象的当前代数。 |
GetGeneration(WeakReference) |
返回指定弱引用的目标的当前代数。 |
GetGeneration(Object)
- Source:
- GC.cs
- Source:
- GC.cs
- Source:
- GC.cs
返回指定对象的当前代数。
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
参数
- obj
- Object
检索其代信息的对象。
返回
或 Int32.MaxValue 的obj
当前代号。
示例
以下示例演示如何使用 GetGeneration 方法确定对象的期限。 然后,该示例执行垃圾回收以清理内存,并在控制台中比较收集前和回收后的内存总计。
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
注解
使用此方法确定对象的期限,然后将该信息与 方法一起使用 Collect ,以强制垃圾回收器在同一代中收集对象。 例如,当你有一组作为组创建且同时变得不可访问的对象时,请使用此方法。
从 .NET 8 开始,此方法可能会返回 MaxValue 在非 GC 堆上分配的对象。 有关详细信息,请参阅 GC。GetGeneration 可能会返回 Int32.MaxValue。
另请参阅
适用于
GetGeneration(WeakReference)
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
返回指定弱引用的目标的当前代数。
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
参数
引用要确定其代数的目标对象的 WeakReference。
返回
或 MaxValue的目标的wo
当前代号。
例外
已经对 wo
执行了垃圾回收。
弱引用的目标已被垃圾回收。
示例
以下示例演示如何使用 GetGeneration 方法来确定弱引用对象的期限。
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
注解
从 .NET 8 开始,此方法可能会返回 MaxValue 在非 GC 堆上分配的对象。 有关详细信息,请参阅 GC。GetGeneration 可能会返回 Int32.MaxValue。