1
我已经创建了一个简单的测试应用程序,如下所示。为什么此代码显示较高的本机内存消耗
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace PerfMonTest
{
class Program
{
private static List<byte[]> byteArray = new List<byte[]>();
static void Main(string[] args)
{
Console.WriteLine("start now");
Console.ReadLine();
Task t1 = Task.Factory.StartNew(() => { Program.ProcessData(); });
Task.WaitAll(new Task[] { t1});
Console.WriteLine("done ...");
Console.ReadLine();
}
private static byte[] GetData()
{
return new byte[1024 * 1024 * 50];
}
public static void ProcessData()
{
for (int i = 0; i < 50000; i++)
{
byteArray.Add(GetData());
Thread.Sleep(500);
Console.WriteLine("GC Memory consumed:" + Convert.ToString((GC.GetTotalMemory(false)/(1024 * 1024))) + " MB");
}
}
}
}
虽然这个应用程序在运行,我也捕捉两个计数器Private Bytes
和# Bytes in all heaps
。其结果如下所示。
根据这一article,该图应该看起来像下面。
我的问题是为什么即使所有堆中的字节数勉强增加,私有字节仍在增加?或者我的代码可能会导致出现在Private bytes
计数器中的任何本机泄漏?