我正在尝试使用32个线程运行并行的foreach循环。
这个foreach循环正在调用:
WorkflowInvoker.Invoke(new MyWorkflow(), myInputDictionary)
并行foreach循环包含大约65000个项目的列表。
在某些时候,大约6000 - 6700个项目调用WorkflowInvoker.Invoke(new MyWorkflow(),myInputDictionary)抛出 带有消息的AccessViolationException: “试图读取或写入受保护的内存。这通常表明其他内存已损坏。”
我尝试了不同的东西,比如创建WorkflowInvoker的实例并将MyWorkflow的实例直接传递给新创建的WorkflowInvoker,然后在类上调用Invoke方法。 也只是将WorkflowInvoker.Invoke称为静态方法,但它没有什么区别。 应用程序每次都在同一点崩溃。由于线程很难调试并精确确定导致问题的原因。
当然,如果我在正常的foreach循环中进行调用,则不会出现问题。
起初我们认为虚拟机内存不足,因为应用程序开始占用所有可用内存,直到它达到总内存的85%(4GB)的限制,然后它似乎崩溃了。所以我们将内存大小更改为1GB,看看我们是否可以在循环中引发异常,但是Windows开始垃圾收集,所以最后异常仍然发生在大约6000到6700个项目的同一点。
非常感谢任何有关我的下一步行动的建议; - )
- 编辑 - 添加了完整的堆栈跟踪 -
at System.Reflection.Emit.ModuleBuilder.nCreateISymWriterForDynamicModule(Module module, String filename)
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModuleInternalNoLock(String name, Boolean emitSymbolInfo, StackCrawlMark& stackMark)
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModuleInternal(String name, Boolean emitSymbolInfo, StackCrawlMark& stackMark)
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule(String name, Boolean emitSymbolInfo)
at System.Activities.Debugger.StateManager.InitDynamicModule(String asmName)
at System.Activities.Debugger.StateManager..ctor(Properties properties, Boolean debugStartedAtRoot)
at System.Activities.Debugger.DebugManager..ctor(Activity root, String moduleNamePrefix, String typeNamePrefix, String auxiliaryThreadName, Boolean breakOnStartup, WorkflowInstance host, Boolean debugStartedAtRoot)
at System.Activities.Debugger.DebugController.EnsureActivityInstrumented(ActivityInstance instance, Boolean primeCurrentInstance)
at System.Activities.Debugger.DebugController.ActivityStarted(ActivityInstance activityInstance)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
at System.Activities.Runtime.ActivityExecutor.ExecuteRootWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.OnExecuteWorkItem(WorkItem workItem)
at System.Activities.Runtime.Scheduler.Callbacks.ExecuteWorkItem(WorkItem workItem)
at System.Activities.Runtime.Scheduler.OnScheduledWork(Object state)
at System.Runtime.Fx.SendOrPostThunk.UnhandledExceptionFrame(Object state)
at System.Activities.WorkflowApplication.PumpBasedSynchronizationContext.WorkItem.Invoke()
at System.Activities.WorkflowApplication.PumpBasedSynchronizationContext.DoPump()
at System.Activities.WorkflowApplication.Invoke(Activity activity, IDictionary`2 inputs, WorkflowInstanceExtensionManager extensions, TimeSpan timeout)
at System.Activities.WorkflowInvoker.Invoke(Activity workflow, IDictionary`2 inputs, TimeSpan timeout, WorkflowInstanceExtensionManager extensions)
at System.Activities.WorkflowInvoker.Invoke(IDictionary`2 inputs)
- 额外编辑 - 添加了有问题的方法 - 对其进行模糊处理以删除businesslogic名称 -
public MyOutputData InvokeMyWorkflow(MyInputData input)
{
IDictionary<string, object> wfInput = new Dictionary<string, object>
{
{ "ArgData", input }
};
MyWorkflow myWorkflow = new MyWorkflow();
try
{
WorkflowInvoker invoker = new WorkflowInvoker(myWorkflow);
IDictionary<string, object> output = invoker.Invoke(wfInput);
return output["ArgData"] == null ? null : (MyOutputData)output["ArgData"];
}
catch (Exception ace)
{
Log.Error(ace.StackTrace);
return null;
}
}