recently i got confused with Task and Thread pool, given the follow codes:
static void Main(string[] args)
{
while (true)
{
Thread.Sleep(1);
Task t = new Task(run);
t.Start();
}
}
static void run()
{
Console.WriteLine(DateTime.Now);
Thread.Sleep(1000 * 3600);
Console.WriteLine("end");
}
According to the code, it will create 1000 tasks per second. I watch the thread count in performance counter, the thread count increase slowly and smoothly, about 1 thread per second. And the memory also increase slowly and smoothly. when the thread count is 1028, it stoped increasing( I think the count is up to the max count of thread of thread pool, and the new task has to been queued.) btw, There's NO OutOfMemoryException.
But when I comment out 'Thread.Sleep(1);' Something strange happened.
In perfromance counter, the thread count growing up very quickly, and the total memory usage takes about 1445Mb. everything happens so fast. at last there's an OutOfMemoryException.
So what make this happen?