简体   繁体   中英

Out Of Memory Exception while running a loop

I am confused at this point I have the following code:

            while (dal.Read())
            {

                if (FiltersAppliedForThisJob(dal.GetInt(0)))
                   continue;
                var j = 0;
                if (SettingsManager.OpsMgrSettings.JobViewServiceList.IsNotNull() &&
                    SettingsManager.OpsMgrSettings.JobViewServiceList.Count > 0)
                    if (!ThisJobHasASupportedService(dal.GetInt(0)))
                        continue;

                var newJobGridRow = AddRowToJobGridWithJobID(dal.GetInt(0),
                                                             ref j);
                BoldThisJobGridRowIfItIsUpdated(newJobGridRow, dal);
                var jobno = dal2.GetJobNo(dal.GetInt(0).ToString());
                if (locns2view.Length > 0)
                    _jobgrid.Rows[newJobGridRow].Visible = locns2view.Contains(jobno.Substring(0, 2));

                var col = 2;
                if (j < _jobgrid.ColumnCount)
                    for (i = 0; i < str1.Length; i++)
                    {
                        if (str1[i].Length <= 0)
                            continue;

                        if (!dal.IsFieldNull(col))
                            PlaceValuesInJobGridCells(dtype, i, dal2, str1, jobno, newJobGridRow, j, dal, col);
                        j++;
                        col++;
                    }
                if (_jobgrid[0, newJobGridRow].Value.ToString() == prevjobid)
                    _jobgrid.Rows[newJobGridRow].Selected = true;
            }

            SortJobGridColumns();
        }

It seems that the out of memory error is coming from the code above. However, if I put a MessageBox before I call FiltersAppliedForThisJob(dal.GetInt(0) , I get no error. For example the code below:

            while (dal.Read())
            {
                MessageBox.Show("Done"); // for debugging purposes

                if (FiltersAppliedForThisJob(dal.GetInt(0)))
                   continue;
                var j = 0;
                if (SettingsManager.OpsMgrSettings.JobViewServiceList.IsNotNull() &&
                    SettingsManager.OpsMgrSettings.JobViewServiceList.Count > 0)
                    if (!ThisJobHasASupportedService(dal.GetInt(0)))
                        continue;

                var newJobGridRow = AddRowToJobGridWithJobID(dal.GetInt(0),
                                                             ref j);
                BoldThisJobGridRowIfItIsUpdated(newJobGridRow, dal);
                var jobno = dal2.GetJobNo(dal.GetInt(0).ToString());
                if (locns2view.Length > 0)
                    _jobgrid.Rows[newJobGridRow].Visible = locns2view.Contains(jobno.Substring(0, 2));

                var col = 2;
                if (j < _jobgrid.ColumnCount)
                    for (i = 0; i < str1.Length; i++)
                    {
                        if (str1[i].Length <= 0)
                            continue;

                        if (!dal.IsFieldNull(col))
                            PlaceValuesInJobGridCells(dtype, i, dal2, str1, jobno, newJobGridRow, j, dal, col);
                        j++;
                        col++;
                    }
                if (_jobgrid[0, newJobGridRow].Value.ToString() == prevjobid)
                    _jobgrid.Rows[newJobGridRow].Selected = true;
            }

            SortJobGridColumns();
        }

Also, if I remove the function call FiltersAppliedForThisJob(dal.GetInt(0) , I don't get an the error.

Here is the function code:

  private bool FiltersAppliedForThisJob(int p)
    {
        var filter_datefrom = SettingsManager.OpsMgrSettings.MainView_DateFrom;
        var filter_dateto = SettingsManager.OpsMgrSettings.MainView_DateTo;
        var filter_status = SettingsManager.OpsMgrSettings.MainView_JobStatus;
        var jobNumTbl = dal.Get_JobNumber(p);
        var jobStatus = GetJobStatus(p);
        var jobdate = jobNumTbl.Date;
        var jobdate_res = false;
        var jobstat_res = false;
        var isFiltered = false;

        if (jobdate.HasValue)
        {
            //both have values
            if (filter_datefrom.HasValue && filter_dateto.HasValue)
            {
                if (DateTime.Compare(jobdate.Value.Date, filter_datefrom.Value.Date) < 0 ||
                    DateTime.Compare(jobdate.Value.Date, filter_dateto.Value.Date) > 0)
                    jobdate_res = true;
            }
            //only datefrom has value
            else if (filter_datefrom.HasValue)
            {
                if (DateTime.Compare(jobdate.Value.Date, filter_datefrom.Value.Date) < 0)
                    jobdate_res = true;
            }
            //only datato has value
            else if (filter_dateto.HasValue)
            {
                if (DateTime.Compare(jobdate.Value.Date, filter_dateto.Value.Date) > 0)
                    jobdate_res = true;
            }
        }

        if (jobStatus.HasValue)
        {
            if (!filter_status.Equals(2))
                if (!filter_status.Equals(jobStatus))
                    jobstat_res = true;
        }

        if (jobdate_res || jobstat_res)
            isFiltered = true;


        return isFiltered;
    }

Not sure if this is enough information. If you need more from me, let me know.

Problem solved. The application was pulling all of the information from the database before filtering, which is a lot of data. I added extra filtering to the query before loading the data. Everything is fine now. Thanks everyone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM