不用担心时间长了会自动释放资源
强制进行垃圾回收。
重载列表
强制对所有代进行垃圾回收。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Shared Sub Collect()
[C#] public static void Collect();
[C++] public: static void Collect();
[JScript] public static function Collect();
强制对零代到指定代进行垃圾回收。
[Visual Basic] Overloads Public Shared Sub Collect(Integer)
[C#] public static void Collect(int);
[C++] public: static void Collect(int);
[JScript] public static function Collect(int);
示例
[Visual Basic, C#, C++] 注意   此示例显示如何使用 Collect 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic] 
Imports SystemNamespace 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 generation 2 only.
            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

解决方案 »

  1.   

    你在KhCx里面读取了什么资源?
      

  2.   

    我的 KhCx 里也就是一些查询而已呀,控件不少。
    每次使用后内存总是增加一次:(
      

  3.   

    代码怎么写呢? 我直接写 KhCx.Collect() 吗? 
    编译出错呀,是不是还是 调用什么呢?
      

  4.   

    我的 KhCx 里也就是一些查询而已呀,控件不少。
    每次使用后内存总是增加一次:(是不是使用了DataTable了!?
    或是DataGrid 
        ArrayList
    之类的东西了,关闭时一定要擦干净,光Clear和Dispose是不行了
      

  5.   

    是否在你的对象不再使用时,对她付值为null会有些帮助呢。
      

  6.   

    在类名前继承 IDisposable 接口,然后重写 Dispose 方法,在方法里面释放你声明的资源,另外在调用这个类的时候请在using块中使用. 例如:
        class DisposeExample:IDisposable
               {
                 protected override void dispose()
                 {
                    //add you code
                 }
                 
               }
      使用的时候:
         using( DisposeExample example = new DisposeExample())
              {
                  // use example code
              }