不用担心时间长了会自动释放资源
强制进行垃圾回收。
重载列表
强制对所有代进行垃圾回收。
受 .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
强制进行垃圾回收。
重载列表
强制对所有代进行垃圾回收。
受 .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
每次使用后内存总是增加一次:(
编译出错呀,是不是还是 调用什么呢?
每次使用后内存总是增加一次:(是不是使用了DataTable了!?
或是DataGrid
ArrayList
之类的东西了,关闭时一定要擦干净,光Clear和Dispose是不行了
class DisposeExample:IDisposable
{
protected override void dispose()
{
//add you code
}
}
使用的时候:
using( DisposeExample example = new DisposeExample())
{
// use example code
}