由于嵌套的for循环而导致内存不足 [英] outofmemoryexception due to nested for loops

查看:202
本文介绍了由于嵌套的for循环而导致内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,我的内存不足了,这是我的代码,用斜体表示.我必须优化这些嵌套循环,以免发生异常.

我在这里告诉背景,我有一个条件,其中我有数据集A,数据集B和数据集C.数据集A是父级&其余的是孩子,我必须合并数据集B&数据集A中的数据集C wrt loanmasterid .


Hi friends i am getting outofmemoryexception, Here is my code which is in italic i have to optimized these nested loops to avoid the exception.

I am telling the background here ,I have a condition in which i have Dataset A,Dataset B and Dataset C.Dataset A is parent & the remaing are childs,I have to merge Dataset B & dataset C in Dataset A wrt loanmasterid.


case Constants.Reports.UHrptLoanSummarybatch:
                  rptData = dbHelper.Get_Loan_Summary_Batch(objReportHelper.CustomerId, objReportHelper.AuditId);//  parent  table
                  rptData2 = dbHelper.Get_Other_Comments_Batch(objReportHelper.CustomerId, objReportHelper.AuditId);
                  rptData4 = dbHelper.Get_Audit_Details_Batch(objReportHelper.CustomerId, objReportHelper.AuditId);

                  rptData.DataSetName = "spReport_LoanSummaryBatch_edit";

                  foreach (DataColumn col in rptData2.Tables[0].Columns)
                  {
                      if(!rptData.Tables[0].Columns.Contains(col.ColumnName))
                          rptData.Tables[0].Columns.Add(col.ColumnName);
                  }

                  foreach (DataColumn col in rptData4.Tables[0].Columns)
                  {
                      if(!rptData.Tables[0].Columns.Contains(col.ColumnName))
                          rptData.Tables[0].Columns.Add(col.ColumnName);
                  }

                  DataRow [] dRows;
                  DataRow parentRow;
                  DataRow newRow;

                  //for (int i = 0; i < rptData.Tables[0].Rows.Count; i++)
                  //{
                  //    parentRow = rptData.Tables[0].Rows[i];

                  //    dRows = rptData2.Tables[0].Select("loanmasterid=" + parentRow["loanmasterid"].ToString());

                  //    foreach (DataRow childRow in dRows)
                  //    {
                  //        newRow = rptData.Tables[0].NewRow();

                  //        foreach (DataColumn childCOl in rptData2.Tables[0].Columns)
                  //        {
                  //            newRow[childCOl.ColumnName] = childRow[childCOl.ColumnName];
                  //        }

                  //        rptData.Tables[0].Rows.Add(newRow);

                  //    }
                  //}

                  for (int i = 0; i < rptData.Tables[0].Rows.Count; i++)
                  {
                      parentRow = rptData.Tables[0].Rows[i];

                      dRows = rptData4.Tables[0].Select("loanmasterid=" + parentRow["loanmasterid"].ToString());

                      foreach (DataRow childRow in dRows)
                      {
                         newRow = rptData.Tables[0].NewRow();

                          foreach (DataColumn childCOl in rptData4.Tables[0].Columns)
                          {
                              newRow[childCOl.ColumnName] = childRow[childCOl.ColumnName];
                          }

                          rptData.Tables[0].Rows.Add(newRow);

                      }
                  }






                  objReportDataSource = new ReportDataSource("spReport_LoanSummaryBatch_edit", rptData4.Tables[0]);



                  objReportDataSource4 = new ReportDataSource("spReport_LoanSummaryBatch_edit", rptData4.Tables[0]);





                  ReportViewer1.LocalReport.ReportPath = "Reports/UHrptLoanSummarybatch.rdlc";

                  break;

推荐答案

Um.我并不感到惊讶.

您的代码有效地
Um. I''m not surprised.

Your code is effectively
int j = 2;
for (int i = 0; i < j; i++)
   {
   j++;
   }

,因为在您的for循环内,您向rptData.Tables[0]添加了可变数量的行,然后使用表中的行数来控制循环.因此,如果您到达某一行导致将自身副本添加到表中的位置,那么它将开始一个无限循环,并且由于每次都要创建新对象而将耗尽内存. >
我不知道您要做什么,但我不禁以为您会以错误的方式来做!

because inside your for loop you add a variable number of rows to rptData.Tables[0] and then use the number of rows in the table to control the loop. So if you ever get to the position where a row causes a copy of itself to be added to the table, then it will start an infinite loop, and you will run out of memory because you create new objects each time round.

I don''t know what you are trying to do, but I can''t help thinking that you are going about it the wrong way!


这篇关于由于嵌套的for循环而导致内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆