GC 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
控制系统垃圾回收器(一种自动回收未使用内存的服务)。
public ref class GC abstract sealed
public ref class GC sealed
public static class GC
public sealed class GC
type GC = class
Public Class GC
Public NotInheritable Class GC
- 继承
-
GC
示例
以下示例使用多个 GC 方法获取有关未使用对象的块的生成和内存信息,并将其打印到控制台。 然后收集未使用的对象,并显示生成的内存总计。
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 强制垃圾回收。
垃圾回收由以下步骤组成:
垃圾回收器搜索托管代码中引用的托管对象。
垃圾回收器尝试完成未引用的对象。
垃圾回收器释放未引用和回收其内存的对象。
本主题包含下列部分:
垃圾回收器和非托管资源
在回收期间,如果垃圾回收器在托管代码中找到对对象的一个或多个引用,则垃圾回收器将不会释放对象。 但是,垃圾回收器无法识别对非托管代码中对象的引用,并且可能会释放在非托管代码中独占使用的对象,除非显式阻止这样做。 该方法 KeepAlive 提供了一种机制,可防止垃圾回收器收集仍在非托管代码中使用的对象。
除了托管内存分配之外,垃圾回收器的实现不会维护有关对象持有的资源的信息,例如文件句柄或数据库连接。 当类型使用在回收类型实例之前必须释放的非托管资源时,该类型可以实现终结器。
在大多数情况下,终结器是通过重写 Object.Finalize 方法来实现的;但是,用 C# 或 C++ 编写的类型实现析构函数,编译器将转换为重写 Object.Finalize。 在大多数情况下,如果对象具有终结器,垃圾回收器会在释放对象之前调用它。 但是,在所有情况下,垃圾回收器都不需要调用终结器:例如,该方法 SuppressFinalize 显式阻止调用对象的终结器。 此外,垃圾回收器不需要使用特定线程来终结对象,或保证为相互引用但可用于垃圾回收的对象调用终结器的顺序。
在必须在特定时间释放资源的情况下,类可以实现 IDisposable 接口,其中包含 IDisposable.Dispose 执行资源管理和清理任务的方法。 实现 Dispose 的类必须指定为类协定的一部分,如果类使用者调用该方法来清理对象,则此类使用者必须指定此类协定的一部分。 默认情况下,垃圾回收器不调用 Dispose 该方法;但是,该方法的 Dispose 实现可以调用类中的 GC 方法来自定义垃圾回收器的最终化行为。
有关对象最终化和释放模式的详细信息,请参阅 清理非托管资源。
对象老化和代系
公共语言运行时中的垃圾回收器支持使用几代的对象老化。 生成是内存中对象相对年龄的度量单位。 对象的生成号或年龄指示对象所属的代系。 最近创建的对象是较新一代的一部分,其生成数比应用程序生命周期早期创建的对象少。 最近一代中的对象位于第 0 代中。 垃圾回收器的此实现支持三代对象、第 0 代、1 代和 2。 可以检索属性的值 MaxGeneration ,以确定系统支持的最大生成数。
对象老化允许应用程序针对特定的一组代而不是要求垃圾回收器评估所有代系。 包含generation
参数的方法的Collect重载允许指定要垃圾回收的最旧一代。
禁止垃圾回收
从 .NET Framework 4.6 开始,垃圾回收器支持在执行垃圾回收可能会对应用性能产生不利影响的关键路径期间使用的 GC 区域延迟模式。 无 GC 区域延迟模式要求指定可以分配的内存量,而不会干扰垃圾回收器。 如果运行时可以分配该内存,则在执行关键路径中的代码时,运行时不会执行垃圾回收。
通过调用其中一个重载来定义无 GC 区域的关键路径的 TryStartNoGCRegion开头。 通过调用 EndNoGCRegion 方法指定其关键路径的结尾。
不能嵌套对方法的 TryStartNoGCRegion 调用,并且仅当运行时当前没有 GC 区域延迟模式时,才应调用 EndNoGCRegion 该方法。 换句话说,不应在第一次方法调用后多次调用 TryStartNoGCRegion (,后续调用不会成功) ,并且不应期望 EndNoGCRegion 调用成功,只是因为第一次调用 TryStartNoGCRegion 成功。
属性
MaxGeneration |
获取系统当前支持的最大代数。 |
方法
AddMemoryPressure(Int64) |
通知运行时在安排垃圾回收时应考虑分配大量的非托管内存。 |
AllocateArray<T>(Int32, Boolean) |
分配数组。 |
AllocateUninitializedArray<T>(Int32, Boolean) |
如果可能,则在跳过零初始化时分配一个数组。 |
CancelFullGCNotification() |
取消注册垃圾回收通知。 |
Collect() |
强制对所有代进行即时垃圾回收。 |
Collect(Int32) |
强制对 0 代到指定代进行即时垃圾回收。 |
Collect(Int32, GCCollectionMode) |
强制在 GCCollectionMode 值所指定的时间对 0 代到指定代进行垃圾回收。 |
Collect(Int32, GCCollectionMode, Boolean) |
在由 GCCollectionMode 值指定的时间,强制对 0 代到指定代进行垃圾回收,另有数值指定回收是否应该为阻碍性。 |
Collect(Int32, GCCollectionMode, Boolean, Boolean) |
在由 GCCollectionMode 值指定的时间,强制对 0 代到指定代进行垃圾回收,另有数值指定回收应该为阻碍性还是压缩性。 |
CollectionCount(Int32) |
返回已经对对象的指定代进行的垃圾回收次数。 |
EndNoGCRegion() |
结束无 GC 区域延迟模式。 |
GetAllocatedBytesForCurrentThread() |
获取自当前线程的生存期开始以来分配给它的总字节数。 |
GetGCMemoryInfo() |
获取垃圾回收的内存信息。 |
GetGCMemoryInfo(GCKind) |
获取垃圾回收的内存信息。 |
GetGeneration(Object) |
返回指定对象的当前代数。 |
GetGeneration(WeakReference) |
返回指定弱引用的目标的当前代数。 |
GetTotalAllocatedBytes(Boolean) |
获取在进程生命周期内分配的字节计数。 返回的值不包括任何本机分配。 |
GetTotalMemory(Boolean) |
检索当前认为要分配的字节数。 一个参数,指示此方法是否可以等待较短间隔再返回,以便系统回收垃圾和终结对象。 |
GetTotalPauseDuration() |
获取自进程开始以来在 GC 中暂停的总时间量。 |
KeepAlive(Object) |
引用指定对象,使其从当前例程开始到调用此方法的那一刻为止均不符合进行垃圾回收的条件。 |
RegisterForFullGCNotification(Int32, Int32) |
指定当条件支持完整垃圾回收以及回收完成时,应引发垃圾回收通知。 |
RemoveMemoryPressure(Int64) |
通知运行时已释放非托管内存,在安排垃圾回收时不需要再考虑它。 |
ReRegisterForFinalize(Object) |
请求系统调用指定对象的终结器,此前已为该对象调用 SuppressFinalize(Object)。 |
SuppressFinalize(Object) |
请求公共语言运行时不要调用指定对象的终结器。 |
TryStartNoGCRegion(Int64) |
如果指定数量的内存可用,则在关键路径执行期间尝试禁止垃圾回收。 |
TryStartNoGCRegion(Int64, Boolean) |
如果指定数量的内存可用,则在关键路径执行期间尝试禁止垃圾回收;并在初始没有足够内存可用的情况下,控制垃圾回收器是否进行完整的阻碍性垃圾回收。 |
TryStartNoGCRegion(Int64, Int64) |
如果指定数量的内存可用于大对象堆和小对象堆,则在关键路径执行期间尝试禁止垃圾回收。 |
TryStartNoGCRegion(Int64, Int64, Boolean) |
如果指定数量的内存可用大对象堆和小对象堆,则在关键路径执行期间尝试禁止垃圾回收;并在初始没有足够内存可用的情况下,控制垃圾回收器是否进行完整的阻碍性垃圾回收。 |
WaitForFullGCApproach() |
返回已注册通知的状态,用于确定公共语言运行时是否即将引发完整、阻碍性垃圾回收。 |
WaitForFullGCApproach(Int32) |
在指定的超时期限内,返回已注册通知的状态,用于确定公共语言运行时是否即将引发完整、阻碍性垃圾回收。 |
WaitForFullGCApproach(TimeSpan) |
在指定的超时期限内,返回已注册通知的状态,用于确定公共语言运行时是否即将引发完整、阻碍性垃圾回收。 |
WaitForFullGCComplete() |
返回已注册通知的状态,用于确定公共语言运行时引发的完整、阻碍性垃圾回收是否已完成。 |
WaitForFullGCComplete(Int32) |
在指定的超时期限内,返回已注册通知的状态,用于确定公共语言运行时引发的完整、阻碍性垃圾回收是否已完成。 |
WaitForFullGCComplete(TimeSpan) |
返回有关阻止垃圾回收是否已完成的已注册通知的状态。 可以无限期等待完整集合。 |
WaitForPendingFinalizers() |
挂起当前线程,直到处理终结器队列的线程清空该队列为止。 |