c# - Garbage Collector too slow in foreach loop? -


i have foreach loop operations images. getting outofmemoryexception when running code 50+ images; because image instances 15+ mb each.

var files = directory.getfiles(path).tolist();  foreach (var file in files) {     image image = new bitmap(file);      //do operations } 

i removed main logic because problem still exists small piece of code. when add gc.collect(); in foreach loop problem gone , don't exception.

my question is: garbage collector slow clean images aren't needed anymore without calling collect method or missing else?

never noticed problem before. never thought there problem because //do operations part needs ~1 second each image. should enough time garbage collector thought.

perhaps should work using:

var files = directory.getfiles(path).tolist();  foreach (var file in files) {     using (image image = new bitmap(file))     {         // work     } } 

this way bitmap disposed after iteration


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -