locked
Batch processing bitmaps, running out of memory! RRS feed

  • Question

  • I have created this (I am new to C#, please dont judge!), to check the size and resolutions of images.

    private void check_image_file(string file_to_check)
            {
                float vresn;
                float hresn;
                int tilewidth;
                int tileheight;
                Bitmap tile = new Bitmap(file_to_check);
    
                vresn = tile.VerticalResolution;
                hresn = tile.VerticalResolution;
                if (vresn != 254) { Console.WriteLine("Vertical res for file " + file_to_check + " not 254"); }
                if (hresn != 254) { Console.WriteLine("Horizontal res for file " + file_to_check + " not 254"); }
    
                tilewidth = tile.Width;
                tileheight = tile.Height;
                if (tilewidth != 4000) { Console.WriteLine("Width for file " + file_to_check + " not 4000"); }
                if (tileheight != 4000) { Console.WriteLine("Height for file " + file_to_check + " not 4000"); }
    I am calling this over 2000 times but when I do, I run out of memory!  The image files are quite big so suspect it is not releasing the memory used for each file before starting again for the next one??  Maybe not but any help would be much appreciated

    Sunday, October 25, 2020 11:45 AM

Answers

  • Make sure that you call Dispose on the Bitmap so that it is freed:

    tile.Dispose();

    If you are new to C#, a good thing to learn is that whenever a class implements the IDisposable interface it is a good practice to Dispose it when you finish using it, so that it releases any non-managed resources that it might be using. And there is a nice language construct to make this simple, which is the using statement (not to be confused with the using directive):

    using (Bitmap tile = new Bitmap(file_to_check))
    {
        // here do things with tile
    }
    // Upon exiting the block the compiler automatically invokes tile.Dispose()
    
    

    • Proposed as answer by BonnieBMVP Sunday, October 25, 2020 1:36 PM
    • Marked as answer by Mister S Monday, October 26, 2020 11:23 AM
    Sunday, October 25, 2020 1:01 PM

All replies

  • Make sure that you call Dispose on the Bitmap so that it is freed:

    tile.Dispose();

    If you are new to C#, a good thing to learn is that whenever a class implements the IDisposable interface it is a good practice to Dispose it when you finish using it, so that it releases any non-managed resources that it might be using. And there is a nice language construct to make this simple, which is the using statement (not to be confused with the using directive):

    using (Bitmap tile = new Bitmap(file_to_check))
    {
        // here do things with tile
    }
    // Upon exiting the block the compiler automatically invokes tile.Dispose()
    
    

    • Proposed as answer by BonnieBMVP Sunday, October 25, 2020 1:36 PM
    • Marked as answer by Mister S Monday, October 26, 2020 11:23 AM
    Sunday, October 25, 2020 1:01 PM
  • Documentation says that “Out of memory” sometimes denotes an invalid or unsupported image. To investigate the problem, try this modification:

       Bitmap tile = null;

       try

       {

          tile = new Bitmap( file_to_check );

       }

       catch( OutOfMemoryException )

       {

          Console.WriteLine( "Bad file: " + file_to_check );

       }

    Put a breakpoint inside the catch block and see if the issue always happens for some particular files even if there is enough free memory.



    • Edited by Viorel_MVP Sunday, October 25, 2020 3:01 PM
    Sunday, October 25, 2020 2:55 PM