c# - Garbage Collector too slow in foreach loop? -
this question has answer here:
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
Post a Comment