GC.Collect 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
强制进行垃圾回收。
重载
Collect() |
强制对所有代进行即时垃圾回收。 |
Collect(Int32) |
强制对 0 代到指定代进行即时垃圾回收。 |
Collect(Int32, GCCollectionMode) |
强制在 GCCollectionMode 值所指定的时间对 0 代到指定代进行垃圾回收。 |
Collect(Int32, GCCollectionMode, Boolean) |
在由 GCCollectionMode 值指定的时间,强制对 0 代到指定代进行垃圾回收,另有数值指定回收是否应该为阻碍性。 |
Collect(Int32, GCCollectionMode, Boolean, Boolean) |
在由 GCCollectionMode 值指定的时间,强制对 0 代到指定代进行垃圾回收,另有数值指定回收应该为阻碍性还是压缩性。 |
Collect()
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
强制对所有代进行即时垃圾回收。
public:
static void Collect();
public static void Collect ();
static member Collect : unit -> unit
Public Shared Sub Collect ()
示例
以下示例演示如何使用 Collect 方法对所有代内存执行集合。 代码生成大量未使用的对象,然后调用 Collect 方法从内存中清除它们。
using namespace System;
const int maxGarbage = 1000;
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;
}
}
void main()
{
// Put some objects in memory.
MakeSomeGarbage();
Console::WriteLine("Memory used before collection: {0:N0}",
GC::GetTotalMemory( false ) );
// Collect all generations of memory.
GC::Collect();
Console::WriteLine("Memory used after full collection: {0:N0}",
GC::GetTotalMemory( true ) );
}
// The output from the example resembles the following:
// Memory used before collection: 79,392
// Memory used after full collection: 52,640
using System;
class MyGCCollectClass
{
private const int maxGarbage = 1000;
static void Main()
{
// Put some objects in memory.
MyGCCollectClass.MakeSomeGarbage();
Console.WriteLine("Memory used before collection: {0:N0}",
GC.GetTotalMemory(false));
// Collect all generations of memory.
GC.Collect();
Console.WriteLine("Memory used after full collection: {0:N0}",
GC.GetTotalMemory(true));
}
static void MakeSomeGarbage()
{
Version vt;
// Create objects and release them to fill up memory with unused objects.
for(int i = 0; i < maxGarbage; i++) {
vt = new Version();
}
}
}
// The output from the example resembles the following:
// Memory used before collection: 79,392
// Memory used after full collection: 52,640
open System
let maxGarbage = 1000
let makeSomeGarbage () =
// Create objects and release them to fill up memory with unused objects.
for _ = 1 to maxGarbage do
Version() |> ignore
// Put some objects in memory.
makeSomeGarbage()
printfn $"Memory used before collection: {GC.GetTotalMemory false:N0}"
// Collect all generations of memory.
GC.Collect()
printfn $"Memory used after full collection: {GC.GetTotalMemory true:N0}"
// The output from the example resembles the following:
// Memory used before collection: 79,392
// Memory used after full collection: 52,640
Class MyGCCollectClass
Private Const maxGarbage As Integer = 1000
Shared Sub Main()
'Put some objects in memory.
MyGCCollectClass.MakeSomeGarbage()
Console.WriteLine("Memory used before collection: {0:N0}",
GC.GetTotalMemory(False))
'Collect all generations of memory.
GC.Collect()
Console.WriteLine("Memory used after full collection: {0:N0}",
GC.GetTotalMemory(True))
End Sub
Shared 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
End Sub
End Class
' The output from the example resembles the following:
' Memory used before collection: 79,392
' Memory used after full collection: 52,640
注解
使用此方法尝试回收所有无法访问的内存。 它对所有代系执行阻塞性垃圾回收。
所有对象,无论它们在内存中已存在多长时间,都考虑用于收集;但是,不会收集托管代码中引用的对象。 使用此方法可强制系统尝试回收最大可用内存量。
从 .NET Framework 4.5.1 开始,可以通过 (在调用 Collect 方法之前将 属性设置为 GCSettings.LargeObjectHeapCompactionModeGCLargeObjectHeapCompactionMode.CompactOnce 来压缩 LOH) 大型对象堆,如以下示例所示。
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();
GCSettings.LargeObjectHeapCompactionMode <- GCLargeObjectHeapCompactionMode.CompactOnce
GC.Collect()
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce
GC.Collect()
另请参阅
适用于
Collect(Int32)
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
强制对 0 代到指定代进行即时垃圾回收。
public:
static void Collect(int generation);
public static void Collect (int generation);
static member Collect : int -> unit
Public Shared Sub Collect (generation As Integer)
参数
- generation
- Int32
最后一代进行垃圾回收次数。
例外
generation
无效。
示例
下面的示例演示如何使用 Collect 方法在内存的各个层上执行集合。 代码生成大量未使用的对象,然后调用 Collect 方法从内存中清除它们。
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
注解
使用此方法尝试回收无法访问的内存。 但是,使用此方法并不能保证回收指定代系中的所有不可访问内存。
如果实现了对象老化,则垃圾回收器不会收集代号高于指定代系的对象。 如果未实现对象老化,则垃圾回收器在垃圾回收期间会考虑所有对象。
MaxGeneration使用 属性确定参数的最大generation
有效值。
若要让垃圾回收器考虑所有对象而不考虑其代系,请使用不采用任何参数的此方法版本。 若要让垃圾回收器基于 GCCollectionMode 设置回收对象,请使用 GC.Collect(Int32, GCCollectionMode) 方法重载。
另请参阅
适用于
Collect(Int32, GCCollectionMode)
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
强制在 GCCollectionMode 值所指定的时间对 0 代到指定代进行垃圾回收。
public:
static void Collect(int generation, GCCollectionMode mode);
public static void Collect (int generation, GCCollectionMode mode);
static member Collect : int * GCCollectionMode -> unit
Public Shared Sub Collect (generation As Integer, mode As GCCollectionMode)
参数
- generation
- Int32
最后一代进行垃圾回收次数。
- mode
- GCCollectionMode
例外
示例
以下示例使用 Optimized 设置强制第 2 代对象进行垃圾回收。
using System;
class Program
{
static void Main(string[] args)
{
GC.Collect(2, GCCollectionMode.Optimized);
}
}
open System
GC.Collect(2, GCCollectionMode.Optimized)
Class Program
Public Shared Sub Main()
GC.Collect(2, GCCollectionMode.Optimized)
End Sub
End Class
注解
mode
使用 参数指定是应立即进行垃圾回收,还是仅在回收对象的最佳时间时才进行。 使用此方法并不能保证回收指定代系中的所有不可访问内存。
若要在应用程序的关键时段调整垃圾回收的侵入性,请 LatencyMode 设置 属性。
垃圾回收器不会收集代号高于 参数指定的 generation
对象。
MaxGeneration使用 属性确定 的最大有效值generation
。
若要让垃圾回收器考虑所有对象而不考虑其代系,请使用不采用任何参数的此方法版本。
若要让垃圾回收器将对象回收到指定的对象代次,请使用 GC.Collect(Int32) 方法重载。 指定最大生成数时,将收集所有对象。
另请参阅
适用于
Collect(Int32, GCCollectionMode, Boolean)
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
在由 GCCollectionMode 值指定的时间,强制对 0 代到指定代进行垃圾回收,另有数值指定回收是否应该为阻碍性。
public:
static void Collect(int generation, GCCollectionMode mode, bool blocking);
public static void Collect (int generation, GCCollectionMode mode, bool blocking);
static member Collect : int * GCCollectionMode * bool -> unit
Public Shared Sub Collect (generation As Integer, mode As GCCollectionMode, blocking As Boolean)
参数
- generation
- Int32
最后一代进行垃圾回收次数。
- mode
- GCCollectionMode
- blocking
- Boolean
true
执行阻碍性垃圾回收;false
在可能的情况下执行后台垃圾回收。
例外
注解
下表总结了 和 blocking
参数的mode
交互:
mode |
blocking 为 true |
blocking 为 false |
---|---|---|
Forced 或 Default | 尽快执行阻塞回收。 如果后台集合正在进行且 generation 为 0 或 1,则 Collect(Int32, GCCollectionMode, Boolean) 该方法会立即触发阻止集合,并在收集完成后返回。 如果后台集合正在进行且 generation 为 2,该方法将等待后台集合完成,触发阻止的第 2 代集合,然后返回 。 |
尽快执行回收。 Collect(Int32, GCCollectionMode, Boolean) 方法请求执行后台回收,但这并没有保证;阻止式回收仍可执行,具体视环境而定。 如果后台回收正在进行,该方法将立即返回。 |
Optimized | 可能会执行阻止式回收,具体视垃圾回收器的状态和 generation 参数而定。 垃圾回收器会尽量提供最佳性能。 |
根据垃圾回收器的状态,有时可执行回收。 Collect(Int32, GCCollectionMode, Boolean) 方法请求执行后台回收,但这并没有保证;阻止式回收仍可执行,具体视环境而定。 垃圾回收器会尽量提供最佳性能。 如果后台回收正在进行,该方法将立即返回。 |
如果对 方法的Collect(Int32, GCCollectionMode, Boolean)调用执行完全阻止垃圾回收,则还可以通过在调用 Collect 方法之前将 GCSettings.LargeObjectHeapCompactionMode 属性设置为 来GCLargeObjectHeapCompactionMode.CompactOnce压缩大型对象堆。
适用于
Collect(Int32, GCCollectionMode, Boolean, Boolean)
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
在由 GCCollectionMode 值指定的时间,强制对 0 代到指定代进行垃圾回收,另有数值指定回收应该为阻碍性还是压缩性。
public:
static void Collect(int generation, GCCollectionMode mode, bool blocking, bool compacting);
public static void Collect (int generation, GCCollectionMode mode, bool blocking, bool compacting);
static member Collect : int * GCCollectionMode * bool * bool -> unit
Public Shared Sub Collect (generation As Integer, mode As GCCollectionMode, blocking As Boolean, compacting As Boolean)
参数
- generation
- Int32
最后一代进行垃圾回收次数。
- mode
- GCCollectionMode
- blocking
- Boolean
true
执行阻碍性垃圾回收;false
在可能的情况下执行后台垃圾回收。
- compacting
- Boolean
true
表示压缩小对象堆;false
表示仅进行清理。
注解
如果 blocking
为 false
,则 GC 决定是执行后台回收还是阻止垃圾回收。 如果 compacting
为 true
,则执行阻止垃圾回收。
如果 compacting
为 true
,则运行时 (SOH) 压缩小型对象堆。 除非 将 属性设置为 GCLargeObjectHeapCompactionMode.CompactOnce,否则GCSettings.LargeObjectHeapCompactionMode不会压缩 LOH) (大型对象堆。 请注意,这包括所有阻止垃圾回收,而不仅仅是完全阻止垃圾回收。
可以调用 Collect(Int32, GCCollectionMode, Boolean, Boolean) 方法将托管堆减小到尽可能小的大小,如以下代码片段所示。
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect(2, GCCollectionMode.Forced, true, true);
GCSettings.LargeObjectHeapCompactionMode <- GCLargeObjectHeapCompactionMode.CompactOnce
GC.Collect(2, GCCollectionMode.Forced, true, true)
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce
GC.Collect(2, GCCollectionMode.Forced, True, True)
true
为 compacting
参数指定 可保证压缩、完全阻止垃圾回收。 将 GCSettings.LargeObjectHeapCompactionMode 属性设置为 GCLargeObjectHeapCompactionMode.CompactOnce 可确保压缩 LOH 和 SOH。