GC 类

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

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public NotInheritable Class GC
用法
可对静态类的成员直接进行访问,无需类的实例。
public static class GC
public ref class GC abstract sealed
public final class GC
public final class GC

备注

此类中的方法影响何时对对象进行垃圾回收以及何时释放对象所分配的资源。此类中的属性提供以下信息:系统可用内存总量、分配给对象的内存的周期类别(代)。

垃圾回收器跟踪并回收托管内存中分配的对象。垃圾回收器定期执行垃圾回收以回收分配给没有有效引用的对象的内存。当使用可用内存不能满足内存请求时,垃圾回收会自动进行。或者,应用程序可以使用 Collect 方法强制进行垃圾回收。

垃圾回收由以下步骤组成:

  1. 垃圾回收器搜索托管代码中引用的托管对象。

  2. 垃圾回收器尝试完成没有被引用的对象。

  3. 垃圾回收器释放没有被引用的对象并回收它们的内存。

在回收期间,如果垃圾回收器在托管代码中找到对某对象的一个或多个引用,则不会释放该对象。然而,垃圾回收器不识别非托管代码中对对象的引用,因此,除非明确禁止,否则它有可能释放非托管代码中以独占方式使用的对象。KeepAlive 方法提供一种机制,该机制可防止垃圾回收器回收在非托管代码中仍使用的对象。

除托管内存分配外,垃圾回收器的实现不维护有关一个对象所保持的资源(如文件句柄或数据库连接)的信息。当某个类型使用的非托管资源在回收该类型的实例之前必须释放时,该类型可以实现终结器。

多数情况下,终结器通过重写 Object.Finalize 方法来实现,但是,用 C# 或 C++ 编写的类型实现析构函数,由编译器将析构函数转换为对 Object.Finalize 的重写。多数情况下,如果某对象有终结器,则垃圾回收器会在释放该对象前调用其终结器。不过,垃圾回收器并不是在所有情况下都需要调用终结器;例如,当 SuppressFinalize 方法显式禁止调用终结器时,垃圾回收器就不需要调用终结器。此外,不需要垃圾回收器使用特定线程来完成对象,也不需要垃圾回收器为相互引用、但能以其他方式用于垃圾回收的对象保证终结器的调用顺序。

在资源必须在特定时间释放的方案中,类可以实现 IDisposable 接口,该接口包含执行资源管理和清理任务的 IDisposable.Dispose 方法。实现 Dispose 的类作为它们的类协定的一部分,必须指定类使用者是否需要及在什么时候调用该方法来清理对象。默认情况下,垃圾回收器并不调用 Dispose 方法,然而 Dispose 方法的实现可以调用 GC 类中的方法来自定义垃圾回收器的完成行为。

建议垃圾回收器使用代来支持对象老化,但这不是必需的。代是对象在内存中相对存现时期的度量单位。对象的代数或存在时期指示对象所属的代。较近创建的对象属于较新的代,比在应用程序生命周期中较早创建的对象的代数低。最近代中的对象位于零代中。

给实现者的说明 垃圾回收器的此实现支持三代对象。 使用 MaxGeneration 确定系统所支持的最大代数。对象老化允许应用程序针对一组特定的代进行垃圾回收,而不需要垃圾回收器计算所有代。

示例

Imports System

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
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();
            }
        }
    }
}
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 ) );
}
package GCCollectIntExample; 

import System.* ;

class MyGCCollectClass
{
    private static final long maxGarbage = 1000;

    public static void main(String[] args)
    {
        MyGCCollectClass myGCCol = new MyGCCollectClass();

        // Determine the maximum number of generations the system
        // garbage collector currently supports.
        Console.WriteLine("The highest generation is {0}", 
            System.Convert.ToString(GC.get_MaxGeneration()));
        myGCCol.MakeSomeGarbage();

        // Determine which generation myGCCol object is stored in.
        Console.WriteLine("Generation: {0}", 
            System.Convert.ToString(GC.GetGeneration(myGCCol)));

        // Determine the best available approximation of the number 
        // of bytes currently allocated in managed memory.
        Console.WriteLine("Total Memory: {0}", 
            System.Convert.ToString(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}", 
            System.Convert.ToString(GC.GetGeneration(myGCCol)));
        Console.WriteLine("Total Memory: {0}", 
            System.Convert.ToString(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}", 
            System.Convert.ToString(GC.GetGeneration(myGCCol)));
        Console.WriteLine("Total Memory: {0}", 
            System.Convert.ToString(GC.GetTotalMemory(false)));
        Console.Read();
    } //main

    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();
        }
    } //MakeSomeGarbage
} //MyGCCollectClass

继承层次结构

System.Object
  System.GC

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

GC 成员
System 命名空间