【发布时间】:2011-06-07 20:49:34
【问题描述】:
大约有 1000 个任务正在运行,但有时我会收到以下由任务调度程序引发的内存不足异常。 可能是什么原因以及如何避免。
System.Threading.Tasks.TaskSchedulerException: An exception was thrown by a TaskScheduler. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
at System.Threading.Thread.Start(StackCrawlMark& stackMark)
at System.Threading.Thread.Start(Object parameter)
at System.Threading.Tasks.ThreadPoolTaskScheduler.QueueTask(Task task)
at System.Threading.Tasks.Task.ScheduleAndStart(Boolean needsProtection)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ScheduleAndStart(Boolean needsProtection)
at System.Threading.Tasks.Task.InternalStartNew(Task creatingTask, Object action, Object state, CancellationToken cancellationToken, TaskScheduler scheduler, TaskCreationOptions options, InternalTaskOptions internalOptions, StackCrawlMark& stackMark)
at System.Threading.Tasks.TaskFactory.StartNew(Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
at App.StartReadSocketTask()
【问题讨论】:
-
原因很可能是你的内存不足。
-
也许你的 1k 任务?请发布一些代码。您似乎同时使用了太多任务。
-
我会说你的内存不足。您必须向我们展示一些实际运行并耗尽内存的代码才能获得帮助。
-
还有,你有多少内存?
-
每个线程需要多大的栈?我也想知道
TaskScheduler.Default.MaximumConcurrencyLevel设置为什么。
标签: c# multithreading task out-of-memory