<分区>
考虑以下示例:
static void DoNothing()
{
signal.WaitOne();
}
static void Main(string[] args)
{
signal = new ManualResetEvent(false);
List<Thread> threads = new List<Thread>();
try
{
while (true)
{
Console.WriteLine($"{threads.Count}, Memory:{Process.GetCurrentProcess().PrivateMemorySize64 / (1024 * 1024)}");
var thread = new Thread(DoNothing);
thread.Start();
threads.Add(thread);
}
}
catch (OutOfMemoryException)
{
Console.WriteLine($"Out of memory at: {threads.Count}");
signal.Set();
}
threads.ForEach(t => t.Join());
Console.WriteLine("Finished.");
Console.ReadLine();
}
代码被编译为 32 位进程。
我发现它在为 .NET 3.5 和 4.x 编译时表现不同。我只更改了 Target 框架的版本。
为 NET 3.5 编译时,内存大约耗尽。每个线程创建 1 MB。这是预期的,因为默认堆栈大小为 1MB ( https://msdn.microsoft.com/en-us/library/windows/desktop/ms686774(v=vs.85).aspx )
但是,当为 .NET 4.x 编译时,内存消耗的速度约为。每个线程创建 100KB,即 1MB 的 1/10。
默认堆栈大小在 .NET 3.5 和 4.x 之间是否发生了变化?
我是在Windows 10上做实验,会不会和Windows版本有关?