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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -