下面提供一种简单的方案,实现对代码块对进程工作集产生影响(造成的GC次数和堆内存的分配)的监管,以便更好地把控代码的性能。
1 int collectCount = GC.CollectionCount(2); //获取自启动进程以来0代进行的垃圾回收次数 2 long totalMemory = GC.GetTotalMemory(true); //托管内存中当前所分配字节数的可用的最佳近似值。 3 //TODO 4 //监管代码块对进程工作集得影响,如果 countVariation / memoryVariation 过大的话,就要对代码块进行优化了 5 int countVariation = GC.CollectionCount(2) - collectCount; 6 long memoryVariation = GC.GetTotalMemory(true) - totalMemory;