相关文章推荐
宽容的凉茶  ·  Oracle ...·  1 年前    · 
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)
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 压缩大型对象堆。

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)

mode 使用参数指定垃圾回收是否应立即发生,或者仅当回收对象的时间最佳时。 使用此方法不能保证回收指定生成中的所有不可访问内存。

若要在应用程序中的关键时段调整垃圾回收的侵入性,请设置该 LatencyMode 属性。

垃圾回收器不会收集生成数高于参数指定的 generation 对象。 使用 MaxGeneration 属性确定最大有效值 generation

若要让垃圾回收器考虑所有对象,而不考虑其生成,请使用此方法的版本,该方法不采用任何参数。

若要让垃圾回收器将对象回收到指定的对象生成,请使用 GC.Collect(Int32) 方法重载。 指定最大生成时,将收集所有对象。

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)

false 如果是 blocking ,GC 决定是执行后台还是阻止垃圾回收。 true 如果是 compacting ,它将执行阻止垃圾回收。

true 如果是 compacting ,运行时会压缩小型对象堆, (SOH) 。 除非属性设置为 GCLargeObjectHeapCompactionMode.CompactOnce (LOH) , GCSettings.LargeObjectHeapCompactionMode 否则不会压缩大型对象堆。 请注意,这包括所有阻止垃圾回收,而不仅仅是完全阻止垃圾回收。

可以调用 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 压缩。

以下示例演示如何使用 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() 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() static void Collect(int generation);
public static void Collect (int generation);
static member Collect : int -> unit
Public Shared Sub Collect (generation As Integer)

以下示例演示如何使用 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}" 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) 方法重载。