c# - Why use GC.Collect here? -


i know has been asked zillion times before, , know prevalent answer "don't it", which, limited knowledge, totally support. however, working on else's code , happens use gc.collect in situation in can hardly see use it. still giving coder benefit of doubt, , perhaps can explain him intention was. needless say, coder not available, used work in company , happened inherit code.

so have class, implemented dispose() method(see @ end of code), , calls gc.collect inside it. class has method generating csv file datatable, , streamwriter.close() called @ end of method. nevermind inheritance class errormanager, barely contains 2 members(string , int) , basic operations them.

public class exportcsv : errormanager {     public exportcsv(typeuser type) { }      public string createcsv(datatable dt, string filename)     {         string retval = "";         retval = filename;          streamwriter sw = new streamwriter(filename, false);          try         {             int icolcount = dt.columns.count;              // scrive nomi dei campi nella prima riga.             (int = 0; < icolcount; i++)             {                 sw.write(dt.columns[i]);                 if (i < icolcount - 1)                 {                     sw.write(",");                 }             }             sw.write(sw.newline);              // write rows.             foreach (datarow dr in dt.rows)             {                 (int = 0; < icolcount; i++)                 {                     if (!convert.isdbnull(dr[i]))                     {                         if (dt.columns[i].datatype == typeof(string))                             sw.write("\"" + dr[i].tostring().replace("\"", "\"\"") + "\"");                         else                             sw.write(dr[i].tostring().replace(",", "."));                          //scrive il valore del campo                         sw.write(dr[i].tostring());                      }                     if (i < icolcount - 1)                     {                         sw.write(",");                     }                 }                 sw.write(sw.newline);             }             sw.close();         }         catch (exception ex)         {             errorcode = 1;             errordescription = ex.message;             retval = "";         }                 {             sw.close();         }          return retval;     }      #region dispose members      public void dispose()     {         gc.collect();     }      #endregion } 

so question just, on earth gc.collect() doing here???

sorry flaws in question, have used stackoverflow long time search other's questions, have barely asked myself before.

it's just possible made sense. well, "made sense" overly generous, might have fixed something.

if objects created reading through datatable , creating strings based on them large in number, , if large batch of these createcsv() operations happened in close succession , if these createcsv() operations called on different instances of exportcsv each disposed of (whether explicitly or via using) call gc.collect() might seem magically speed things or prevent out of memory issues.

in such case have code creating lot of objects can end cases spike in objects being allocated , available garbage collection outpaces gc expecting. , when happens periodically calling gc.collect() explicitly can beneficial. rare, can 1 of few cases explicit collection necessary. (simple test; if machine doesn't grind halt without it, isn't necessary).

now consider:

for(var = 0; != somelargenumber; ++i) {   var table = gettablesfromsomesource();   var filename = getfilenamesfromsomesource();   using(var exporter = new exportcsv(someargument))   {     exporter.createcsv(table, filename);   } // `dispose()` called here, calling gc.collect() } 

if case, fact cycle of different exportcsv instances being disposed mean gc.collect() being called periodically have fixed things. no different if called somewhere else in loop. indeed somewhere else in loop better; being more controllable (you add count not call every single loop) it's being called closer actual logic that's requiring (the need relates there being loop in turn involves lot of allocation , release, not object being disposed).

for fun, coming along , looking @ code in exportcsv , seeing gc.collect() in dispose() pointless , removing cause memory issue of such spike in use recur. they'd put gc.collect() in , magically fix issue. gc.collect() have become "more magic" switch.

that possibility marginal. if case, suggest moving gc.collect() belongs. suggest trying if(++loopcount % somenumber == 0){gc.collect();} reduce number of calls; higher somenumber less unnecessary calls greater risk of triggering memory problems explicit calls there prevent, optimum best found profiling on real data.

on other hand, may didn't garbage collection , disposal different things, along lines of several questions can find here people start off thinking dispose() has freeing managed memory, , seemed idea on basis. in case it's hurting gc performance no reason; take out. if class isn't public api take out unnecessary idisposable implementation entirely, though that's breaking change if public api.


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -