当我在我的程序中使用 Parallel.ForEach 时,我发现有些线程似乎永远不会完成。事实上,它一遍又一遍地产生新线程,这是我没有预料到也绝对不想要的行为。
我能够使用以下代码重现此行为,就像我的“真实”程序一样,都大量使用处理器和内存(.NET 4.0 代码):
public class Node
{
public Node Previous { get; private set; }
public Node(Node previous)
{
Previous = previous;
}
}
public class Program
{
public static void Main(string[] args)
{
DateTime startMoment = DateTime.Now;
int concurrentThreads = 0;
var jobs = Enumerable.Range(0, 2000);
Parallel.ForEach(jobs, delegate(int jobNr)
{
Interlocked.Increment(ref concurrentThreads);
int heavyness = jobNr % 9;
//Give the processor and the garbage collector something to do...
List<Node> nodes = new List<Node>();
Node current = null;
for (int y = 0; y < 1024 * 1024 * heavyness; y++)
{
current = new Node(current);
nodes.Add(current);
}
TimeSpan elapsed = DateTime.Now - startMoment;
int threadsRemaining = Interlocked.Decrement(ref concurrentThreads);
Console.WriteLine("[{0:mm\\:ss}] Job {1,4} complete. {2} threads remaining.", elapsed, jobNr, threadsRemaining);
});
}
}
当在我的四核上运行时,它最初以 4 个并发线程开始,正如您所期望的那样。然而,随着时间的推移,越来越多的线程被创建。最终,该程序会抛出 OutOfMemoryException:
[00:00] Job 0 complete. 3 threads remaining.
[00:01] Job 1 complete. 4 threads remaining.
[00:01] Job 2 complete. 4 threads remaining.
[00:02] Job 3 complete. 4 threads remaining.
[00:05] Job 9 complete. 5 threads remaining.
[00:05] Job 4 complete. 5 threads remaining.
[00:05] Job 5 complete. 5 threads remaining.
[00:05] Job 10 complete. 5 threads remaining.
[00:08] Job 11 complete. 5 threads remaining.
[00:08] Job 6 complete. 5 threads remaining.
...
[00:55] Job 67 complete. 7 threads remaining.
[00:56] Job 81 complete. 8 threads remaining.
...
[01:54] Job 107 complete. 11 threads remaining.
[02:00] Job 121 complete. 12 threads remaining.
..
[02:55] Job 115 complete. 19 threads remaining.
[03:02] Job 166 complete. 21 threads remaining.
...
[03:41] Job 113 complete. 28 threads remaining.
<OutOfMemoryException>
上面实验的内存使用图如下:
(屏幕截图是荷兰语;上半部分表示处理器使用情况,下半部分表示内存使用情况。)如您所见,几乎每次垃圾收集器妨碍时都会产生一个新线程(可以看出在内存使用量下降的情况下)。
谁能解释为什么会发生这种情况,以及我能做些什么?我只想让 .NET 停止产生新线程,并首先完成现有线程......