Run GC.Collect synchronously

前端 未结 4 1115
情深已故
情深已故 2020-12-06 16:33

GC.Collect appears to start the garbage collection in a background thread, and then return immediately. How can I run GC.Collect synchronously -- i

4条回答
  •  星月不相逢
    2020-12-06 17:12

    You can use GC.RegisterForFullGCNotification, trigger a full collection with GC.Collect(GC.MaxGeneration) and then the GC.WaitForFullGCComplete and GC.WaitForPendingFinalizers methods, but make sure to use this in your tests only, they should not be used for production code.

提交回复
热议问题