C# Parallel.ForEach的OutOfMemory异常
我有一个控制台应用程序,它作为32位进程运行(我们不能将其更改为64位),并且正在抛出内存不足异常。我们在下游进程中跟踪了较小的内存泄漏(与实体框架存储库相关),但应用程序不应跨越2GB内存。 我想了解的一点是,有时应用程序处理2500条记录,而有时它在100条记录时失败。(当此应用程序运行时,服务器内存利用率低于60%) 我正在使用Parallel.forEach,并使用Parallel选项将线程数控制为4。是否有人建议在单个线程上创建应用程序可能会解决此问题。 (该应用程序在任何较低的环境中都没有失败,只是在生产中失败,这给了我们一段非常艰难的时间)。 下面是代码片段。 提前感谢,, 罗希特C# Parallel.ForEach的OutOfMemory异常,c#,C#,我有一个控制台应用程序,它作为32位进程运行(我们不能将其更改为64位),并且正在抛出内存不足异常。我们在下游进程中跟踪了较小的内存泄漏(与实体框架存储库相关),但应用程序不应跨越2GB内存。 我想了解的一点是,有时应用程序处理2500条记录,而有时它在100条记录时失败。(当此应用程序运行时,服务器内存利用率低于60%) 我正在使用Parallel.forEach,并使用Parallel选项将线程数控制为4。是否有人建议在单个线程上创建应用程序可能会解决此问题。 (该应用程序在任何较低的环境中
protected override void Execute(IEnumerable参数)
{
if(argument.Any())
{
var-EnrollmentRequests=argument.GroupBy(c=>c.CaseNumber.ToList();
已选择的PlanPremiumDetails请求;
注册响应注册响应;
布尔状态;
Common.Status.TotalAutoEnrollRecords=EnrollmentRequests.Count;
Common.StartTimer();
Action processRequest=eRequest=>
{
国际案件编号;
列出登记的个人;
尝试
{
字符串错误消息;
caseNumber=eRequest.Key;
if(eRequest.Any(f=>f.failedMConrollmentId>0))
{
请求=失败的CoreRequest(eRequest,案例编号);
enrollmentResponse=InvokeRollment(请求);
if(enrollmentResponse.OverallStatus==false&&enrollmentResponse.ErrorInformationMA!=null)
{
StringBuilder消息=新建StringBuilder();
如果(enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps!=null)
{
messages.Append(enrollmentResponse.ErrorInformationMA.ErrorInformationPostCaps.Message);
异常innerExp=enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps.InnerException;
if(innerExp!=null)
{
//messages.Append(enrollmentResponse.ErrorInformationMA.ErrorInformationPostCaps.InnerException.Message);
做
{
messages.Append(innerExp.Message);
innerExp=innerExp.InnerException;
}
while(innerExp!=null);
}
}
其他的
{
if(enrollmentResponse.ErrorInformation!=null&&enrollmentResponse.ErrorInformation.InnerErrorInfo!=null)
{
foreach(enrollmentResponse.ErrorInformation.InnerErrorInfo中的var msg)
{
Append(string.Format(@“ErrorCode:{0},ErrorMessage:{1}”,msg.ErrorCode,msg.ErrorMessage));
}
}
}
errorMessage=Convert.ToString(消息);
Common.Errors.Add(新异常(String.Format(ConfigurationManager.AppSettings[“FailedErrorText”]、caseNumber、errorMessage)、enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps));
taskStatus=GenerateTask(eRequest、caseNumber、false、errorMessage);
如果(任务状态)
{
UpdateTriggerStatus(案例编号,“Y”);
}
其他的
{
UpdateTriggerStatus(案例编号,“N”);
常见.Errors.Add(新异常(String.Format(ConfigurationManager.AppSettings[“FailedErrorText”]、案例编号、“任务创建”));
}
}
其他的
{
Common.Status.Success=(Common.Status.Success+1);
UpdateTriggerStatus(案例编号,“Y”);
}
}
其他的
{
enrolledIndividuals=eRequest.Select(p=>new enrolledIndividuals
{
IndividualId=p.IndividualId,
EdgTraceId=p.EdgTraceId,
EDGNumber=p.EDGNumber
}).ToList();
请求=新选择的PlanPremiumDetails()
{
EmployerId=0,
案例编号=案例编号,
CallBackId=Common.ApplicationName,
SourceSystem=EnrollmentLibrary.SourceSystem.MAAUTOASSIGN,
AutoAssignMCOIndividuals=已登记的个人
};
enrollmentResponse=InvokeRollment(请求);
if(enrollmentResponse.OverallStatus==false&&enrollmentResponse.ErrorInformationMA!=null)
{
StringBuilder消息=新建StringBuilder();
如果(enrollmentResponse.ErrorInformationMA.ErrorInformationPostM
protected override void Execute(IEnumerable<PlanPremiumDetails> argument)
{
if (argument.Any())
{
var EnrollmentRequests = argument.GroupBy(c => c.CaseNumber).ToList();
SelectedPlanPremiumDetails request;
EnrollmentResponse enrollmentResponse;
bool taskStatus;
Common.Status.TotalAutoEnrollRecords = EnrollmentRequests.Count;
Common.StartTimer();
Action<IGrouping<int,PlanPremiumDetails>> processRequest = eRequest =>
{
int caseNumber;
List<EnrolledIndividual> enrolledIndividuals;
try
{
string errorMessage;
caseNumber = eRequest.Key;
if (eRequest.Any(f => f.FailedMCOEnrollmentId > 0))
{
request = FailedMcoRequest(eRequest, caseNumber);
enrollmentResponse = InvokeEnrollment(request);
if (enrollmentResponse.OverallStatus == false && enrollmentResponse.ErrorInformationMA != null)
{
StringBuilder messages = new StringBuilder();
if (enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps != null)
{
messages.Append(enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps.Message);
Exception innerExp = enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps.InnerException;
if (innerExp != null)
{
// messages.Append(enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps.InnerException.Message);
do
{
messages.Append(innerExp.Message);
innerExp = innerExp.InnerException;
}
while (innerExp != null);
}
}
else
{
if (enrollmentResponse.ErrorInformationMA != null && enrollmentResponse.ErrorInformationMA.InnerErrorInfo != null)
{
foreach (var msg in enrollmentResponse.ErrorInformationMA.InnerErrorInfo)
{
messages.Append( string.Format(@"ErrorCode: {0}, ErrorMessage: {1} ",msg.ErrorCode,msg.ErrorMessage));
}
}
}
errorMessage = Convert.ToString(messages);
Common.Errors.Add(new Exception(String.Format(ConfigurationManager.AppSettings["FailedEnrollErrorText"], caseNumber, errorMessage), enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps));
taskStatus = GenerateTask(eRequest, caseNumber, false, errorMessage);
if (taskStatus)
{
UpdateTriggerStatus(caseNumber, "Y");
}
else
{
UpdateTriggerStatus(caseNumber, "N");
Common.Errors.Add(new Exception(String.Format(ConfigurationManager.AppSettings["FailedEnrollErrorText"], caseNumber, "Task Creation")));
}
}
else
{
Common.Status.Success = (Common.Status.Success + 1);
UpdateTriggerStatus(caseNumber, "Y");
}
}
else
{
enrolledIndividuals = eRequest.Select(p => new EnrolledIndividual
{
IndividualId = p.IndividualId,
EdgTraceId = p.EdgTraceId,
EDGNumber = p.EDGNumber
}).ToList();
request = new SelectedPlanPremiumDetails()
{
EmployerId = 0,
CaseNumber = caseNumber,
CallBackId = Common.ApplicationName,
SourceSystem = EnrollmentLibrary.SourceSystem.MAAUTOASSIGN,
AutoAssignMCOIndividuals = enrolledIndividuals
};
enrollmentResponse = InvokeEnrollment(request);
if (enrollmentResponse.OverallStatus == false && enrollmentResponse.ErrorInformationMA != null)
{
StringBuilder messages = new StringBuilder();
if (enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps != null)
{
messages.Append(enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps.Message);
Exception innerExp = enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps.InnerException;
if (innerExp != null)
{
// messages.Append(enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps.InnerException.Message);
do
{
messages.Append(innerExp.Message);
innerExp = innerExp.InnerException;
}
while (innerExp != null);
}
}
else
{
if (enrollmentResponse.ErrorInformationMA != null && enrollmentResponse.ErrorInformationMA.InnerErrorInfo.Count != null)
{
foreach (var msg in enrollmentResponse.ErrorInformationMA.InnerErrorInfo)
{
messages.Append(string.Format(@"ErrorCode: {0}, ErrorMessage: {1} ", msg.ErrorCode, msg.ErrorMessage));
}
}
}
errorMessage = Convert.ToString(messages);
Common.Errors.Add(new Exception(String.Format(ConfigurationManager.AppSettings["AutoEnrollErrorText"], caseNumber, errorMessage), enrollmentResponse.ErrorInformationMA.ErrorInformationPostMcaps));
}
else
{
// Update status to be saved in InterfaceSummary table.
Common.Status.Success = (Common.Status.Success + 1);
}
}
}
catch(Exception ex)
{
Common.Errors.Add(new Exception(String.Format(ConfigurationManager.AppSettings["AutoEnrollErrorText"], eRequest.Key, ex.Message), ex));
}
};
}
int dParallelism = Convert.ToInt32(ConfigurationManager.AppSettings["DegreeofParallelism"]);
Parallel.ForEach(EnrollmentRequests,
new ParallelOptions() { MaxDegreeOfParallelism = dParallelism > 0 ? dParallelism : 1 },
processRequest);