WaitForFullGCComplete 与(WaitForPendingFinalizers + 收集)?

WaitForFullGCComplete vs (WaitForPendingFinalizers + collect)?

我无法理解 WaitForFullGCCompleteWaitForPendingFinalizers + collect 之间的区别。

我已经知道当创建一个新对象时(它有一个终结器)——在finalization queue.[=71 中创建对该对象的引用=] 现在 - 当 GC .collect 发生并发现应该收集对象时,finalization queue 的引用移动 f-reachable queue

队列中的这些引用被视为 root 。现在 — 一个特殊的线程在 f-reachable queue 和 运行 每个对象的 finalize 方法处从睡眠中醒来。

注意这是在 collect 阶段之后完成的。所以只有下一次我们 运行 GC.collect 对象才会真正消失。

如果我们想等到 f-reachable queue's 线程完成所有 finalize 方法 - 我们应该调用:WaitForPendingFinalizers

现在如果我们想要完全收集,那么我们应该再次 运行 GC.collect!

如果是这样 - full GC 似乎可以通过以下方式进行:

GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);

那我为什么需要 GC.WaitForFullGCComplete() 呢?

docs 说:

Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime has completed.

问题

我不明白:我已经知道 Gc 什么时候完成,就在那个时候:

GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
//HERE <--

那么我什么时候应该使用该方法而不是我的代码?

注意

(我看到它的用法 and here)

但它们的用法并不完整,因为 - 更不用说还有 另一个必须成对调用的 方法 :

通过 C# 4 从 CLR:

Note that you should always call the WaitForFullGCApproach and WaitForFullGCComplete methods in pairs because the CLR handles them as pairs internally.

除非我误解了,否则您所指出的 link 中的两种用法都是错误的。正如您通过 C# 从 CLR 引用的那样,WaitForFullGCApproachWaitForFullGCComplete 都应该成对使用。

WaitForFullGCCompleteCollect + WaitForPendingFinalizers 非常不同。它的用法完全不同。

假设您正在编写一个每秒处理数千个请求的高性能、低延迟服务器应用程序。显然,服务器会分配大量内存,这可能会触发完整的 gc(阻塞!),这对我们来说是个坏消息。

是的,完整的 gc 需要一段时间才能完成,直到那时您无法处理来自客户端的请求(因为您的线程将被挂起)。高性能应用程序在高峰时段容忍完整的 gc 是不可取的。

在这种情况下,如果您想将进一步的请求重定向到可以处理它们的其他服务器,您将分别使用 GC.RegisterForFullGCNotificationGC.WaitForFullGCApproachGC.WaitForFullGCComplete 方法。

WaitForFullGCApproach 会一直阻塞直到 GC 决定进行 full gc,这是一个通知,让你主动重定向请求或采取一些行动。

然后WaitForFullGCComplete会阻塞直到full gc完成。这是通知您重新开始处理请求。

GC.RegisterForFullGCNotification docs 提供了一个很好的例子并更好地解释了它。