C# Memory usage spike with static method System.Drawing.Image.FromFile -


so have program loads bunch of files listview in preparation upload. happen have taskmanager open when loaded 600 photos stress test , check responses times. shocked when memory spiked on 1.2 gigs. have small helper class validates files pictures , causing spike. if not call method, memory usage constant. not sure why happening.

i use background worker load files , update observable collection bound listview.

    // select file populate worker:     // ------------------------------------------------------------------------------------     private void select_file_populate_start(object sender, doworkeventargs e)     {     .     .     .         // each file selected, add listview form.         (int = 0; < selected_files.length; i++)         {             // makes sure same form not add list again.             bool duplicate = false;              // enumerates through observable collection see if there match file path.             foreach (filedata entry in selected_files_data)             {                 if (entry.path == selected_files[i])                 {                     duplicate = true;                 }             }              // if file not duplicate check validity of file.             if (duplicate == false && tools.validate_image(selected_files[i]))             {                 selected_files_buffer.add(selected_files[i]);             }         }     .     .     .     } 

in snip above , call helper method tools.validate_image(selected_files[i]) validate file photo.

here code that:

public static class tools {     public static string filetype { get; set; }      // checks make sure image valid.     // ----------------------------------------------------------------------------------------------     public static bool validate_image(string filename)     {         try         {             system.drawing.imaging.imageformat file_format = system.drawing.image.fromfile(filename).rawformat;              if (file_format.equals(system.drawing.imaging.imageformat.jpeg))             {                 filetype = ".jpg";                  return true;             }             else if (file_format.equals(system.drawing.imaging.imageformat.png))             {                 filetype = ".png";                  return true;             }             else             {                 return false;             }          }         catch //(exception ex)         {             return false;         }     // ---------------------------------------------------------------------------------------------- } 

.net managed environment, meaning unreferenced objects remain in memory until .net garbage collector kicks in. memory usage can surely spike until happens.

the gc either kicks in automatically (usually not frequently), or being triggered manually, can this:

system.gc.collect(); 

i recommend not often, may hurt performance. 'too often' depends on situation. try doing every 10, 50 or 100 files processed, while checking both max memory usage , cpu time spent in total.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -