我正在尝试使用 .Net AppDomain 类的MonitoringSurvivedMemorySize属性。
唯一的问题是我看不到我的应用程序从该属性接收的值如何符合 msdn 上给出的属性描述。
我观察到的差异包括属性返回的值我知道太低,因为返回的值与先前调用返回的值不同,并且在这两个调用之间,AppDomain 引用的内存量从未像新的返回值。
我在 SO、google 和 connect.microsoft.com 中搜索了“AppDomain Monitoring”,但找不到任何似乎与上述特定问题相关的内容。
为了确保这与我的应用程序的其他部分没有任何关系,我使用以下代码创建了一个新的 ConsoleApplication:
class Program
{
static void Main(string[] args)
{
Console.WindowWidth = 133;
AppDomain.MonitoringIsEnabled = true;
var Queue = new Queue<byte[]>();
var BlockSize = (int)1e+6;
var ToggleAllocation = true;
while (true)
{
if (Console.KeyAvailable)
{
var Input = Console.ReadKey(true);
if (Input.Key == ConsoleKey.Escape)
{
break;
}
if (Input.Key == ConsoleKey.C)
{
GC.Collect();
Console.WriteLine("Collect");
}
ToggleAllocation = !ToggleAllocation;
}
Thread.Sleep(100);
if (!ToggleAllocation)
{
continue;
}
Queue.Enqueue(new byte[BlockSize]);
while (100 < Queue.Count)
{
Queue.Dequeue();
}
var ExpectedLowerBound = Queue.Count * BlockSize;
var MonitoringSurvivedMemorySize = AppDomain.CurrentDomain.MonitoringSurvivedMemorySize;
var MonitoringTotalAllocatedMemorySize = AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize;
Console.WriteLine(
"Queue.Count=" + Queue.Count.ToString() +
", SurvivedMemorySize=" + MonitoringSurvivedMemorySize.ToString() +
", TotalAllocatedMemorySize=" + MonitoringTotalAllocatedMemorySize.ToString() +
", ExpectedLowerBound=" + ExpectedLowerBound.ToString());
}
}
}
这个函数只是不时地分配一些内存以确保最终会发生 gc 并打印出 MonitoringSurvivedMemorySize 接收到的值。
为了确保问题不是由于某些调试器“功能”引起的,我将项目切换到默认发布配置并从资源管理器运行程序。
当我运行这个程序时,MonitoringSurvivedMemorySize在一些迭代中返回 0,然后在一些迭代之后(我假设在第一次 gc 之后)一个符合我期望的更高值。但经过更多次迭代后,返回值会跳回零。它还返回不同于零但有时远低的值。
我将目标框架设置为“.NET Framework 4 Client Profile”
我究竟做错了什么?
可以复盘吗?