GC 类

定义

控制系统垃圾回收器(一种自动回收未使用内存的服务)。

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

注解

有关此 API 的详细信息,请参阅 GC 的补充 API 说明

属性

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()

获取自当前线程的生存期开始以来分配给它的总字节数。

GetConfigurationVariables()

获取垃圾回收器使用的配置。

GetGCMemoryInfo()

获取垃圾回收的内存信息。

GetGCMemoryInfo(GCKind)

获取垃圾回收的内存信息。

GetGeneration(Object)

返回指定对象的当前代数。

GetGeneration(WeakReference)

返回指定弱引用的目标的当前代数。

GetTotalAllocatedBytes(Boolean)

获取在进程生命周期内分配的字节计数。 返回的值不包括任何本机分配。

GetTotalMemory(Boolean)

检索不包括碎片的堆大小。 例如,如果总 GC 堆大小为 100mb,碎片(即可用对象占用的空间)占用 40mb,则此 API 将报告 60mb。 一个参数,指示此方法是否可以等待较短间隔再返回,以便系统回收垃圾和终结对象。

GetTotalPauseDuration()

获取自进程开始以来 GC 中暂停的总时间量。

KeepAlive(Object)

引用指定对象,使其从当前例程开始到调用此方法的那一刻为止均不符合进行垃圾回收的条件。

RefreshMemoryLimit()

指示垃圾回收器通过检测系统上的各种内存限制来重新配置自身。

RegisterForFullGCNotification(Int32, Int32)

指定当条件支持完整垃圾回收以及回收完成时,应引发垃圾回收通知。

RegisterNoGCRegionCallback(Int64, Action)

注册在无 GC 区域中分配一定内存量时要调用的回调。

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()

挂起当前线程,直到处理终结器队列的线程清空该队列为止。

适用于

另请参阅