Run GC.Collect synchronously

前端 未结 4 1116
情深已故
情深已故 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 17:24

    Finalizers are run on a dedicated, high-priority background thread. From the background in your post, I gather that you can simply do

    GC.Collect();
    GC.WaitForPendingFinalizers();
    

    The Collect() will schedule any non-rooted instances for finalization and then the thread will wait for the finalizer thread to complete.

提交回复
热议问题