我正在分析代码的关键部分所花费的时间。
//Take a timestamp before
var before = DateTime.UtcNow;
DoCriticalMethod();
//Take a timestamp after
var after = DateTime.UtcNow;
有时我的一些值(value)观明显高于其他值(value)观。
我怀疑这是垃圾收集,所以我想将高值与在此过程中发生垃圾收集这一事实联系起来。
到目前为止我试过这个: 我之前去柜台 :
//Take the number before
int gc2CountBefore = GC.CollectionCount(2);
...
//Take the number after
bool hasgc2occured = (GC.CollectionCount(2) - gc2CountBefore) != 0;
我做得好吗?
最佳答案
“RegisterForFullGCNotification”方法注册在运行时检测到完整垃圾收集即将到来时发出的通知。此通知分为两部分:完全垃圾收集临近时和完全垃圾收集完成时。
要确定何时发出通知,请使用“WaitForFullGCApproach”和“WaitForFullGCComplete”方法。通常,您在 while 循环中使用这些方法来持续获取显示通知状态的“GCNotificationStatus”枚举。
“WaitForFullGCApproach”和“WaitForFullGCComplete”方法旨在协同工作。单独使用其中一个会产生不确定的结果。
关于c# - 如何知道是否在两个检查点之间触发了 gen full 垃圾回收?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21632923/