Parallel.ForEach的OutOfMemory异常

时间:2014-01-21 13:41:45

标签: c#

我有一个控制台应用程序,它运行为32位进程(我们无法将其更改为64位)并且丢失了内存异常。我们在下游进程中跟踪了较小的内存泄漏(与实体框架存储库相关),但是应用程序也不应该跨越2 GB内存。 我想要理解的一件事是有时应用程序处理2500条记录,而有时它在100处失败。(此应用程序运行时服务器内存利用率低于60%)  我正在使用Parallel.forEach并使用ParallelOptions控制线程数为4。任何人都可以建议在单个线程上制作应用程序可以通过任何机会解决问题。 (应用程序在任何较低的环境中都没有失败,只是在生产中并且给我们带来了非常困难的时间)。 以下是代码段。 提前致谢, 罗希特夏尔

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);

0 个答案:

没有答案