c# - Dealing with OutOfMemory Exception -
is there proper way deal out of memory exceptions while looping through large list , adding objects list? proper way go doing this? have large linq query returns around 600k items. go through each item , add object. code below.
static void main(string[] args) { grabdata(); } public static void grabdata() { decimal? totalnetclaim = 0; using (var context = new vscmesnapentities()) { list<drivetimeobject> datafile = new list<drivetimeobject>(); drivetimeobject dt = new drivetimeobject(); datetime paramdate = new datetime(2015, 05, 30); list<viewdrivetimefiledump> dataquery = new list<viewdrivetimefiledump>(); dataquery = (from z in context.viewdrivetimefiledumps select z).tolist(); foreach (var item in dataquery) { decimal? amountchargedparts = dt.getamountchargedparts(item.chrcomponsts.trim(), item.mnstotalparts); decimal? amountchargedpartstax = dt.getamountchargedpartstax(item.chrcomponsts.trim(), item.mnstotalparttax); decimal? amountchargedlabor = dt.getamountchargedlabor(item.chrcomponsts.trim(), item.mnstotallabor); decimal? amountchargedlabortax = dt.getamountchargedlabortax(item.chrcomponsts.trim(), item.mnstotallabortax); int? daysout = dt.getdaysoutclaim(item.intrepairfacilcode, item.dtmcontpurchdate, item.dtmreported); long? milesout = dt.getmilesoutclaim(item.intrepairfacilcode, item.inbincurmiles, item.inborigmiles); decimal? deductible = dt.getdeductible(item.chrcontsts, item.mnsdeduct); decimal? netclaim = dt.getnetclaim(item.chrcomponsts.trim(), item.mnstotalparts, item.mnstotalparttax, item.mnstotallabor, item.mnstotallabortax, item.mnsdeduct); datafile.add(new drivetimeobject { dealernumber = item.chrdlrnum, vscname = item.chvvscname, iclocationnumber = item.iclocationnumber, icregion = item.icregion, identifier = item.chridentifier, contractnumber = item.chrcontnum, vin = item.chrvin, coveragecode = item.cvgcode, claimnum = item.intclaimnum, originalmiles = item.inborigmiles, contractpurchasedate = item.dtmcontpurchdate, incurmiles = item.inbincurmiles, datereported = item.dtmreported, daysoutclaim = daysout, milesout = milesout, repairfacilitynumber = item.intrepairfacilcode, facilityname = item.chvfacilityname, zipfive = item.chrzipfive, facilityadvisor = item.chrfaciladvisor, componentstatus = item.chrcomponsts, componentstatusword = item.compondesc, componentcode = item.chrcomponcode, statusmasterdescription = item.masterdesc, componentdescription = item.chvcompondesc, parts = amountchargedparts, partstax = amountchargedpartstax, labor = amountchargedlabor, labortax = amountchargedlabortax, deductible = deductible, netclaim = netclaim, carriercode = item.intcarriercode, networkstatus = item.networkstatus, addon = item.chraddon, etcdate = item.etc, atcdate = item.atc, labortime = item.realabortime, paiddate = item.dtmpddate, paymentid = item.intpaymentid, batchnumber = item.intbatchnum }); totalnetclaim += netclaim; } context.dispose(); } console.writeline(totalnetclaim); console.readkey(); }
i run out of memory during foreach
loop , wondering how should go adjusting code make work.
the way prevent out of memory not run out of memory. means need rid of objects don't need.
without learning more of use case, hard suggest fix. regardless of that, it's bad practice have many objects in memory run out , crash. only keep in memory need.
one fix not use ram memory, , instead use hard drive memory. ex: can write objects database , rid of them, don't keep them around. considering have 600k objects, these in batches of 10k/25k records. when need objects, can query them. if need calculations objects, recommend doing operations using sql queries.
Comments
Post a Comment