我正在使用 .NET 中的 Monitor 类,所以我得到了一段似乎可以工作的代码,但是当我循环它一段时间时,它会抛出一个OutOfMemoryException
.
我在具有 8 GB RAM 的 64 位 Windows 8 开发人员机器上运行此程序,并且该过程从未占用超过 100 MB 的 RAM 空间。
这是我的代码:
using System;
using System.Threading;
public class Program
{
public static void Main()
{
while (true) {
object theLock = new Object();
Thread threadA = new Thread(() =>
{
Console.WriteLine("Thread A before lock");
lock (theLock)
{
Console.WriteLine("Thread A locking, about to Wait");
Monitor.Wait(theLock);
}
Console.WriteLine("Thread A after lock");
});
Thread threadB = new Thread(() =>
{
Console.WriteLine("Thread B before lock");
lock (theLock)
{
Console.WriteLine("Thread B lockint, about to Pulse");
Monitor.Pulse(theLock);
}
Console.WriteLine("Thread B before lock");
});
threadA.Start();
threadB.Start();
GC.Collect();
}
}
}
我在这里读到这可能是一个碎片问题,我GC.Collect()
在最后添加了。但是我没有分配大块的空间。
然后我决定测量循环在抛出异常之前大约经历了多少次迭代并添加了一个计数器:
using System;
using System.Threading;
public class Program
{
public static void Main()
{
int counter = 0;
while (true) {
Console.WriteLine(counter);
counter++;
object theLock = new Object();
Thread threadA = new Thread(() =>
{
Console.WriteLine("Thread A before lock");
lock (theLock)
{
Console.WriteLine("Thread A locking, about to Wait");
Monitor.Wait(theLock);
}
Console.WriteLine("Thread A after lock");
});
Thread threadB = new Thread(() =>
{
Console.WriteLine("Thread B before lock");
lock (theLock)
{
Console.WriteLine("Thread B lockint, about to Pulse");
Monitor.Pulse(theLock);
}
Console.WriteLine("Thread B before lock");
});
threadA.Start();
threadB.Start();
GC.Collect();
}
}
}
这似乎大大减慢了抛出异常的速度。我测量了 36000 次迭代。