当我从 C# 应用程序中访问某些 PerformanceCounters 时,我看到一些奇怪的行为。例如,当我访问 Process - Private Bytes 时,似乎我得到了很多第 2 代垃圾收集(对于其他进程计数器来说似乎也是如此)。
下面的程序演示了这一点。如果你每 8 秒左右运行一次,我会看到一个 gen2 集合。如果我删除 _privateBytesCounter.NextValue() 我根本看不到任何 gen2 集合。
谁能解释一下吗?
我正在运行 Visual Studio 2015 并针对 .NET 4.5.2。
using System;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
namespace Gen2Collections
{
class Program
{
private static PerformanceCounter _privateBytesCounter;
private static PerformanceCounter _gen2Counter;
static void Main(string[] args)
{
_privateBytesCounter = new PerformanceCounter("Process", "Private Bytes", Process.GetCurrentProcess().ProcessName);
_gen2Counter = new PerformanceCounter(".NET CLR Memory", "# Gen 2 Collections", Process.GetCurrentProcess().ProcessName);
RunMonitor();
Thread.Sleep(60000);
}
private static void RunMonitor()
{
Task.Run(() =>
{
while (true)
{
_privateBytesCounter.NextValue();
Console.WriteLine(_gen2Counter.NextValue());
Thread.Sleep(1000);
}
});
}
}
}
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)