GC.Collect 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
强制垃圾回收。
重载
| 名称 | 说明 |
|---|---|
| Collect() |
强制立即对所有代进行垃圾回收。 |
| Collect(Int32) |
强制从第 0 代到指定代的即时垃圾回收。 |
| Collect(Int32, GCCollectionMode) |
强制从第 0 代到指定代的垃圾回收,一次由值 GCCollectionMode 指定。 |
| Collect(Int32, GCCollectionMode, Boolean) |
强制从第 0 代到指定代的垃圾回收,一次由 GCCollectionMode 值指定,并指定是否应阻止回收的值。 |
| Collect(Int32, GCCollectionMode, Boolean, Boolean) |
强制从第 0 代到指定代的垃圾回收,一次由值 GCCollectionMode 指定,并指定是否应阻止和压缩回收的值。 |
Collect()
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
- 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 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.LargeObjectHeapCompactionMode属性设置为 GCLargeObjectHeapCompactionMode.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
- 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 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
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
强制从第 0 代到指定代的垃圾回收,一次由值 GCCollectionMode 指定。
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
例外
示例
以下示例使用此设置强制第 2 代对象的 Optimized 垃圾回收。
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
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
强制从第 0 代到指定代的垃圾回收,一次由 GCCollectionMode 值指定,并指定是否应阻止回收的值。
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 (如果可能)执行后台垃圾回收。
例外
注解
下表总结了参数的modeblocking交互:
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
- Source:
- GC.CoreCLR.cs
- Source:
- GC.CoreCLR.cs
强制从第 0 代到指定代的垃圾回收,一次由值 GCCollectionMode 指定,并指定是否应阻止和压缩回收的值。
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 仅扫描。
注解
false如果是blocking,GC 决定是执行后台还是阻止垃圾回收。
true如果是compacting,它将执行阻止垃圾回收。
true如果是compacting,运行时会压缩小型对象堆(SOH)。 除非属性 GCSettings.LargeObjectHeapCompactionMode 设置为 GCLargeObjectHeapCompactionMode.CompactOnce,否则不会压缩大型对象堆 (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。